How can you sort five GAA drill times with at most one swap per pass?
Your GAA coach wants these drill times in order, shortest first: eight, three, six, two and five minutes. If you can make at most one swap on your first pass through the list, which two entries should you swap, and when?
Think back to linear search. If you're looking for a drill that took six minutes, you check one entry at a time. You pass eight, then three, and you can stop when you reach six.
Start with eight as your smallest so far. Three is smaller, so remember its position instead. Six changes nothing. Two takes over, and five changes nothing. Only once you've scanned every entry do you know that two is the minimum.
You're scanning nine, four, seven, one, and six for the smallest value. You've just read seven, so two entries are still to come. Pick the position you should be remembering right now.
After the first scan, swap two with eight. Leave two fixed and scan the rest. Three is already next, so skip the swap. On the third pass, swap five with six. Finally, swap six with eight. This way of filling one position at a time is called selection sort.
Start with eight, three, six, two and five. Remember position one first. Three is smaller, so remember position two. Six leaves it unchanged. Two is smaller again, so remember position four. Five leaves it unchanged. Now swap positions one and four to get two, three, six, eight and five.
Now start with six, four, seven, one, and five. Scan all five entries before you change the list. Pick the list that shows what happens after just the first pass of selection sort.
Both traces start with eight, three, six, two and five. On the left, swap eight with three, then three with two. On the right, remember each new minimum's position and swap eight with two only at the end. The lists differ: the left takes two swaps and the right takes one.
With five entries, the scans make four, three, two and one value comparisons. That's ten. In general, multiply the list length by one less than itself, then halve it. Even a sorted list needs that many comparisons. You make at most one swap per pass, so five entries need at most four swaps.
If the question says, 'Write the contents of the list after the first pass of a simple selection sort,' stop after that pass. Here the scan finds one, so swap it with seven and write the entire list, including the entries that stayed put.