ProgrammingVery hard
A selection sort swaps the minimum into place, but only when the minimum is not already in place. How many swaps does it make on {7, 2, 9, 4, 1, 8}?
- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: D. 4
Each pass finds the minimum of the unsorted part. A swap is needed only when that minimum is not at the front. Tracing the passes gives the count shown.
More C Programming questions
- Records are counted into buckets by key 0 to 2 in a stable counting sort. Input: Ann 2, Bob 0, Cy 2, Dee 1, Eve 0. What is the output…
- Keys {4, 7, 2, 9, 6, 1} are split into the even keys first and then the odd keys, keeping each group in input order. What is the result?
- Records (P,5), (Q,9), (R,9), (S,3) are scanned with if (s.score >= best.score) best = s; starting from the first record. Which name is kept?
- Records (Raj,30), (Ana,25), (Raj,22), (Ana,40) are sorted first by age with a stable sort, then by name with another stable sort. What is…
- A swap-based selection sort is run on the records (A,2), (B,2), (C,1) using the key only. What is the final order?
- Upper bound: in the sorted keys 1, 3, 5, 5, 5, 8, 9 (0-based), what index does bisectright / upperbound return for x = 5?