Question: 1. Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A1.
1. Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A1. Then find the second smallest element of A, and exchange it with A2. Continue in this manner for the first n 1elements of A. Write pseudocode for this algorithm, which is known as selection sort. What loop invariant does this algorithm maintain? Why does it need to run for only the first n 1 elements, rather than for all n elements? Give the best-case and worst-case running times of selection sort in -notation.
2. Consider linear search again (see Exercise 2.1-3). How many elements of the in- put sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in -notation? Justify your answers.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
