Data Structures and Algorithms
The array 29, 10, 14, 37, 13 is sorted in ascending order with selection sort, which finds the minimum of the unsorted part and swaps it with the first element of the unsorted part. What is the array after this step has been done twice?
- A10, 13, 14, 37, 29
- B10, 14, 29, 37, 13
- C10, 14, 13, 29, 37
Show answer and explanation
Answer: A. 10, 13, 14, 37, 29
The first step swaps the overall minimum 10 with 29 at the front, giving 10, 29, 14, 37, 13. The second step swaps 13, the minimum from the second element on, with 29 in the second position, giving 10, 13, 14, 37, 29. Selection sort always makes n(n−1)/2 comparisons but at most one swap per step, which can be an advantage when writes are expensive. 10, 14, 29, 37, 13 is the array after insertion sort has inserted two elements, and 10, 14, 13, 29, 37 is the array after two passes of bubble sort.
- A. Correct. 10 and then 13 are swapped to the front.
- B. This is the array after insertion sort has inserted 10 and 14.
- C. This is the array after two passes of bubble sort.