Model answer
Pass i scans n−i−1 remaining candidates, giving (n−1)+(n−2)+…+1=n(n−1)/2 comparisons regardless of input order.
Use the key terms, then explain the reasoning.
Selection sort repeatedly selects the minimum remaining value and places it at the next output position.
Open this in the visual labSelection sort divides the array into a sorted prefix and unsorted suffix. It scans the entire suffix for its minimum, then exchanges that minimum with the first unsorted position.
The comparison count is fixed regardless of input order, but the algorithm uses at most n−1 swaps. This can matter when writes are far more expensive than comparisons.
| Operation | Best | Average | Worst | Extra space |
|---|---|---|---|---|
| Sort | O(n²) | O(n²) | O(n²) | O(1) |
The prefix before i is sorted and contains the i smallest input values.
Open a prompt when you are ready to check your answer.