Complete the following function de nition This function sor

Complete the following function de nition.
/* This function sorts an integer array from smallest to largest using
* the selection sort algorithm.
*
* Parameters:
* int arr[] -- the array to sort
* int count -- the number of items in the array
*
* Returns:
* nothing
*/
void sort(int arr[ ], int count) {
for(int i = 0; i < count - 1; i++) {
int min_index = i;
for(int j = i + 1; j < count; j++) {
if(arr[ j ] < arr[ min_index ]) {
min_index = j;
}
// swap the item at min_index with the item at i

}
}
}

Solution

Here is the filled in code for you:

/* This function sorts an integer array from smallest to largest using
* the selection sort algorithm.
*
* Parameters:
* int arr[] -- the array to sort
* int count -- the number of items in the array
*
* Returns:
* nothing
*/
void sort(int arr[ ], int count) {
for(int i = 0; i < count - 1; i++) {
int min_index = i;
for(int j = i + 1; j < count; j++) {
if(arr[ j ] < arr[ min_index ]) {
min_index = j;
}
           // swap the item at min_index with the item at i
            int temp = arr[min_index];   //Copy the element at min_index to temp.
            arr[min_index] = arr[i];   //Copy the element at index i to array position at min_index.
            arr[i] = temp;               //Copy the element in temp to array position at i.

}
}
}

Complete the following function de nition. /* This function sorts an integer array from smallest to largest using * the selection sort algorithm. * * Parameters

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site