/* selection.c - a sorting algorithm by Nate Weeks */ #include #include #include // function to swap 2 values void swap(int *xp, int *yp){ int temp = *xp; *xp = *yp; *yp = temp; }; // sort an array of length n void selectionSort(int arr[], int n){ int i, j, min_index; // i = index, j = sub-index for (i = 0; i < n-1; i++){ min_index = i; // set the index to be compared with the rest of the array to i for (j = i + 1; j < n; j++){ // loop through the indices greater than i if (arr[j] < arr[min_index]){ min_index = j; // shift the min_index to j if arr[j] is smaller than arr[min_index] }; swap(&arr[min_index], &arr[i]); // swap the value at arr[min_index] and arr[i] }; }; }; void printArray(int arr[], int size){ int i; for(i = 0; i < size; i++){ printf("%d ", arr[i]); }; }; //populate an array of a given length with randomly generated numbers void createArray(int arr[], int size){ int i; for(i=0; i