previous | index | next

Sorting Arrays

If the numbers are in an array, we can sort it "in place" using the Selection Sort algorithm.

In each of the following steps, we scan for the largest element (shown in green) in the unsorted part of the array and "swap" it with the element at the largest index of the unsorted portion.

At each step, the sorted portion is shown in red.

Array     Action
2 6 3 8 5 4 1 7 Find 8; swap with 7
2 6 3 7 5 4 1 8 Find 7; swap with 1
2 6 3 1 5 4 7 8 Find 6; swap with 4
2 4 3 1 5 6 7 8 Find 5; swap with itself
2 4 3 1 5 6 7 8 Find 4; swap with 1
2 1 3 4 5 6 7 8 Find 3; swap with itself
2 1 3 4 5 6 7 8 Find 2; swap with 1
1 2 3 4 5 6 7 8 Done


previous | index | next