Consider sorting n numbers stored in array A by first finding the largest element of A and exchang- ing it with the element in A[n]. Then find the second largest element of A and exchange it with A[n 1]. Continue in this manner for the first n - 1 elements of A. (a) Write a (non-recursive) pseudocode for this algorithm, which is known as selection sort. Give the best-case and worst-case running times of selection sort in 9-notation. ¹Le., function of the form a"nº logº n for constants a ≥1 and b, c ≥ 0. 2This way even if you got part (a) wrong, you can still have correct solution to part (b).

icon
Related questions
Question

Need both parts. ......attempt if you will solve both parts. ...thanks 

Consider sorting n numbers stored in array A by first finding the largest element of A and exchang-
ing it with the element in A[n]. Then find the second largest element of A and exchange it with
A[n 1]. Continue in this manner for the first n - 1 elements of A.
(a)
Write a (non-recursive) pseudocode for this algorithm, which is known as selection
sort. Give the best-case and worst-case running times of selection sort in e-notation.
¹Le., function of the form a" nº logn for constants a ≥ 1 and b, c ≥ 0.
2This way even if you got part (a) wrong, you can still have correct solution to part (b).
(b)
In this question, you will need to prove the correctness of your algorithm from
part (a). In order to do this, you will first state a suitable loop invariant. You will then prove
correctness using this loop invariant and induction.
Transcribed Image Text:Consider sorting n numbers stored in array A by first finding the largest element of A and exchang- ing it with the element in A[n]. Then find the second largest element of A and exchange it with A[n 1]. Continue in this manner for the first n - 1 elements of A. (a) Write a (non-recursive) pseudocode for this algorithm, which is known as selection sort. Give the best-case and worst-case running times of selection sort in e-notation. ¹Le., function of the form a" nº logn for constants a ≥ 1 and b, c ≥ 0. 2This way even if you got part (a) wrong, you can still have correct solution to part (b). (b) In this question, you will need to prove the correctness of your algorithm from part (a). In order to do this, you will first state a suitable loop invariant. You will then prove correctness using this loop invariant and induction.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer