1. Sorting Arrays (Selection Sort)
Selection Sort
Selection sort is another algorithm for arranging the elements in a collection (array)
in a particular order. Here is an illustration of the execution of Selection Sort for
arranging the elements of an array in increasing order:
(For convenience the runs start at 0 instead of 1)
At run 0 swap element at index 0 with smallest element in [1 -- end]
1 1 [2 -- end]
2 2 [3 -- end]
3 3 [4 -- end]
... ... ...
i i [i+1 -- end]
Here is a visualization of the process from Wikipedia (the pink cell is the current smallest,
the purple is the one we are considering for a potential new smallest):
Here is a visualization of Selection Sort through a dance:
Here is another example of the execution of the algorithm:
original: 7 4 1 8 3 2 6 5 (1 and 7 will swap)
do run 1 ↑ [ ↑ ]
after run 1: 1 4 7 8 3 2 6 5 (4 and 2 will swap)
do run 2 ↑ [ ↑ ]
after run 2: 1 2 7 8 3 4 6 5 (7 and 3 will swap)
do run 3 ↑ [ ↑ ]
after run 3: 1 2 3 8 7 4 6 5 (8 and 4 will swap)
do run 4 ↑ [ ↑ ]
after run 4: 1 2 3 4 7 8 6 5 (7 and 5 will swap)
do run 5 ↑ [ ↑]
after run 5: 1 2 3 4 5 8 6 7 (8 and 6 will swap)
do run 6 ↑ [↑ ]
after run 6: 1 2 3 4 5 6 8 7 (8 and 7 will swap)
do run 7 ↑ [↑]
after run 7: 1 2 3 4 5 6 7 8 Done!
* Selection Sort did 7 runs and 7 swaps.
* On the same array Bubble Sort did 5 runs, but more swaps.
Selection Sort Implementation
Just as in Bubble Sort it is convenient to use a helper that accomplishes some
work and simply repeat this procedure enough times until the array is sorted.
The helper procedure in this case finds the index of the smallest element in a
given portion of the array. (If we want to sort the array in decreasing order,
we need to find the index of the biggest element in the relevant portion of the
array.
Selection Sort Observations
We made the following observations about the execution of Selection Sort:
1. After the k-th run (i.e. swapping the k-th item with the smallest neighbor
to the right), the first k items are ordered and in their final spots.
_______________________________ _____________
|__ordered and in final places__|__scrambled__|
2. At each run at most one swap is made -- i.e. each element is swapped at most
once. In comparison, during Bubble Sort an element could be swapped in and out
multiple times throughout the execution of the algorithm.
3. The previous observation suggests that Selection Sort is likely to be faster
than Bubble/Insertion Sort (since it does fewer swaps) when the array is
"highly unordered".
4. The nature of Bubble Sort and Insertion Sort allows them to "detect" whether the array
is sorted. Unfortunately, this is not the case for Selection Sort, so it will carry out
exactly the same number of instructions for find min, i.e. the same number of
comparisons regardless of how "disordered" the array is.
Thus, we expect Selection Sort to be slower than Bubble/Insertion Sort when the array is
close to sorted.