Question: Q 4 . ( a ) The function listed below implements a sorting algorithm called selection sort. Explain how the algorithm works, with reference to

Q4.(a) The function listed below implements a sorting algorithm called selection sort. Explain how the algorithm works, with reference to the program. Demonstrate the operation of the algorithm on the array [684529]. typedef int Key; void selection_sort(Key *array, int n){ int i, j, min, minindex, temp; for(i =0; i < n-1; i++){ min = array[i]; minindex = i; for(j = i +1; j < n; j++){ if (array[j]< min){ min = array[j]; minindex = j; }} temp = array[i]; array[i]= min; array[minindex]= temp; }}(55%)(b) Assuming that we define the complexity of the algorithm to be equal to the number of comparisons made in sorting N elements, derive an equation which expresses the worst-case (i.e. highest) complexity as a function of N. Compare this value with the best-case (i.e. lowest) complexity of the algorithm.

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 Programming Questions!