How many comparisons are needed to sort an array of length 5 if a straight…
2008
How many comparisons are needed to sort an array of length 5 if a straight selection sort is used and array is already in the opposite order?
Answer: C. 10 — Straight selection sort is a non-adaptive comparison sort: in every pass it scans the entire remaining unsorted portion to find the minimum element,…
- A.
1
- B.
5
- C.
10
- D.
20
Attempted by 1183 students.
Show answer & explanation
Correct answer: C
Straight selection sort is a non-adaptive comparison sort: in every pass it scans the entire remaining unsorted portion to find the minimum element, regardless of whether the array is random, sorted, or in reverse order. For an array of length n, this produces exactly n(n-1)/2 comparisons in total — the same count in the best, average, and worst case.
Pass 1: the unsorted portion has all 5 elements, so finding the minimum takes 4 comparisons.
Pass 2: the unsorted portion has shrunk to 4 elements (the first minimum is already placed), so finding the next minimum takes 3 comparisons.
Pass 3: the unsorted portion has 3 elements left, taking 2 comparisons.
Pass 4: the unsorted portion has 2 elements left, taking 1 comparison. After this pass, the last element is in place with no comparison needed.
Adding the comparisons from every pass gives 4 + 3 + 2 + 1 = 10, matching the formula n(n-1)/2 = 5 x 4 / 2 = 10. Because selection sort always scans the full unsorted portion in each pass instead of stopping early when it detects the array is already ordered, this count does not change even though the array starts in reverse (opposite) order.
So a straight selection sort makes 10 comparisons on this array.
Explore the full course: Iocl Engineers Officers Grade A Paper 2