Answered step by step
Verified Expert Solution
Question
1 Approved Answer
To find the largest element in an unsorted array takes n - 1 comparisons, as does finding the smallest element in an unsorted array. Present,
To find the largest element in an unsorted array takes n - 1 comparisons, as does finding the smallest element in an unsorted array. Present, prove correct, and analyses comparisons for an algorithm that uses [3/2n] - 2 comparisons to find both the largest and smallest elements of an unsorted array. (2 points algorithm description, 4 points correctness proof, 2 points correct number of comparisons, 2 points correctly analysing number of comparisons) Say we use the above algorithm to improve selection sort as follows: procedure lmpSelectSort(a_1,a_2,...,a_n: a list of distinct real numbers with n > 2 even) for k:= 1 to n/2 do: begin{do} Use the algorithm from the previous problem to find the locations i of the maximum element and j of the minimum element in the sub-array a[k]....a[n - k + 1]. Swap a[k] and a[i]; swap a[n - k + 1] and a[j] end{do} Return the array a_1,...a_n How many comparisons does this algorithm make? How does this compare to the number of comparisons selection sort makes? To find the largest element in an unsorted array takes n - 1 comparisons, as does finding the smallest element in an unsorted array. Present, prove correct, and analyses comparisons for an algorithm that uses [3/2n] - 2 comparisons to find both the largest and smallest elements of an unsorted array. (2 points algorithm description, 4 points correctness proof, 2 points correct number of comparisons, 2 points correctly analysing number of comparisons) Say we use the above algorithm to improve selection sort as follows: procedure lmpSelectSort(a_1,a_2,...,a_n: a list of distinct real numbers with n > 2 even) for k:= 1 to n/2 do: begin{do} Use the algorithm from the previous problem to find the locations i of the maximum element and j of the minimum element in the sub-array a[k]....a[n - k + 1]. Swap a[k] and a[i]; swap a[n - k + 1] and a[j] end{do} Return the array a_1,...a_n How many comparisons does this algorithm make? How does this compare to the number of comparisons selection sort makes
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started