Question: Question) Selection sort algorithm is given below. 1. for each value of fill from 0 to n-2 2. Find index_of_min, the index of the smallest

Question) Selection sort algorithm is given below. 1. for each value of fill from 0 to n-2 2. Find index_of_min, the index of the smallest element in the unsorted subarray list[fill] through list[n-1] 3. if fill is not the position of the smallest element (index_of_min) 4. Exchange the smallest element with the one at position fill. /* Figure 7.16 Function select_sort */ /* * Finds the position of the smallest element in the subarray * list[first] through list[last]. * Pre: first < last and elements 0 through last of array list are defined. * Post: Returns the subscript k of the smallest element in the subarray; * i.e., list[k] <= list[i] for all i in the subarray */ int get_min_range(int list[], int first, int last); /* * Sorts the data in array list * Pre: first n elements of list are defined and n >= 0 */ void select_sort(int list[], /* input/output - array being sorted */ int n) /* input - number of elements to sort */ { int fill, /* index of first element in unsorted subarray */ temp, /* temporary storage */ index_of_min; /* subscript of next smallest element */ for (fill = 0; fill < n-1; ++fill) { /* Find position of smallest element in unsorted subarray */ index_of_min = get_min_range(list, fill, n-1); /* Exchange elements at fill and index_of_min */ if (fill != index_of_min) { temp = list[index_of_min]; list[index_of_min] = list[fill]; list[fill] = temp; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!