ProgrammingHard
A selection sort on 5 elements (find the minimum, swap into place) makes how many key comparisons in total, for any input?
- A. 4
- B. 5
- C. 10
- D. 7
Correct answer: C. 10
Selection sort always compares every remaining element in each pass, so the comparison count is n(n-1)/2 = 10 regardless of the data.
More C Programming questions
- Bubble sort with an early exit when a pass makes no swap is given an already sorted array of 5 elements. How many comparisons does it make?
- A sorted array of 10^6 keys answers 10^5 queries of the form 'how many keys lie in [L, R]'. Which approach is correct and fast enough?
- In a sorted array with duplicates, which index does a lowerbound search return for the key 5 in [1, 5, 5, 5, 8]?
- Two sorted struct arrays of sizes n and m are merged into a new array. How many elements does the merged array hold?
- A struct array of n records is sorted by key. The code stores a record index inside each record before sorting, so that it can recover the…
- A histogram of sorted keys [1, 1, 2, 2, 2, 3] counts the adjacent equal pairs. How many such pairs are there?