Explore BrainMass

Explore BrainMass

    Insertion Sort and Selection Sort Using C

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Implement insertion sort and selection sort using C programming language:

    1. Generate an array of random integers to be sorted using either insertion sort or selection sort.
    2. Allow the user to specify the number of integers in the array to be sorted in the main function.
    3. Allow the user to specify the range of random integers to be generated.
    4. Output the array of random integers before and after the sorting operation.

    © BrainMass Inc. brainmass.com September 27, 2022, 5:12 pm ad1c9bdddf
    https://brainmass.com/computer-science/c/insertion-sort-selection-591850

    SOLUTION This solution is FREE courtesy of BrainMass!

    This program is an implementation of insertion sort and selection sort using C programming language. The program generates an array of random integers to be sorted using either insertion sort or selection sort. The user can specify the number of integers in the array to be sorted in the main function. The user can also specify the range of random integers to be generated. The program prints the array of random integers before and after the sorting operation.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    /**
    * Generates an array of random integers
    *
    * @param size the size of the integer array
    * @param range the range of the random integers generated
    * @return a pointer to the generated integer array
    */
    int* getIntegerList(int size, int range) {
    srand(time(NULL));
    int *intArray = malloc(size * sizeof (int));
    int i;
    for (i = 0; i < size; i++) {
    *(intArray + i) = rand() % range;
    }

    return intArray;
    }

    /**
    * Selection sorts an array of integers
    *
    * @param arr a pointer to an integer array
    * @param size the size of the integer array
    */
    void selectionSort(int* arr, int size) {
    int i;
    int j;
    for (i = 0; i < size - 1; i++) {
    int smallest = i;
    for (j = i + 1; j < size; j++) {
    if (*(arr + j)<*(arr + smallest)) {
    smallest = j;
    }
    }
    int temp = *(arr + i);
    *(arr + i) = *(arr + smallest);
    *(arr + smallest) = temp;
    }
    }

    /**
    * Inserts an integer into the given array so that
    * the integer is sorted in ascending order from the beginning
    * to pos
    *
    * @param arr a pointer to the integer array
    * @param pos search position, from 0 to pos
    * @param newNum new integer to be added
    */
    void insertInPlace(int * arr, int pos, int newNum) {
    int insertPos = pos - 1;
    while (insertPos >= 0 && newNum<*(arr + insertPos)) {
    *(arr + insertPos + 1) = *(arr + insertPos);
    insertPos--;
    }
    *(arr + insertPos + 1) = newNum;
    }

    /**
    * insertion sorts an array of integers
    *
    * @param arr a pointer to an integer array
    * @param size the size of the integer array
    */
    void insertionSort(int* arr, int size) {
    int i;
    for (i = 1; i < size; i++) {
    insertInPlace(arr, i, *(arr + i));
    }
    }

    /**
    * main function
    *
    * @param argc
    * @param argv
    * @return
    */
    int main(int argc, char** argv) {
    int size = 30; //number of elements in the array to be sorted
    int range = 1000; //random integer range

    int *arr = getIntegerList(size, range);
    int i;
    for (i = 0; i < size; i++) {
    printf("%d", *(arr + i));
    if (i < size - 1) {
    printf(", ");
    }
    }

    printf("n");
    //selectionSort(arr,size);
    insertionSort(arr, size);
    for (i = 0; i < size; i++) {
    printf("%d", *(arr + i));
    if (i < size - 1) {
    printf(", ");
    }
    }
    free(arr);
    return (EXIT_SUCCESS);
    }.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    © BrainMass Inc. brainmass.com September 27, 2022, 5:12 pm ad1c9bdddf>
    https://brainmass.com/computer-science/c/insertion-sort-selection-591850

    ADVERTISEMENT