Sequences
4.5 Selection Sort
In the last section we learned that we can call a method calledsorton a list to sort the items in the list in ascending order. Ascending order is determined by theless than operator as defined on the items. So how does the sorting algorithm work. One of the early sorting algorithms was calledSelection Sortand it serves as a good starting place to understand sorting algorithms. However, this is not the sorting algorithm used by Python. We’ll find out why soon.
The selection sort algorithm is pretty simple to describe. The algorithm begins by finding the smallest value to place in the first position in the list. It does this by doing a linear search through the list and along the way remembering the index of the smallest item it finds. The algorithm uses theguess and checkpattern by first guessing that the smallest item is the first item in the list and then checking the subsequent items to see if it made an incorrect guess. This part of the algorithm is theselectionpart. Theselectfunction does this selection.
4.5.1 Selection Sort’s Select Function
1 def select(seq, start):
2 minIndex = start
3
4 for j in range(start+1, len(seq)):
5 if seq[minIndex] > seq[j]:
6 minIndex = j
7
8 return minIndex
The start argument tells the select function where to start looking for the smallest item. It searches from start to the end of the sequence for the smallest item.
The selection sort algorithm works by finding the smallest item using theselect function and placing that item into the first position of the sequence. Now the value in the first position must be put someplace else. It is simply swapped with the location of the value that is being moved. The algorithm proceeds by next looking for the second smallest value in the sequence. Since the smallest value is now in the first location in the sequence, the selection sort algorithm starts looking from the second position in the list for the smallest value. When the smallest value is found (which is really the second smallest value for the list) the value in the second position and this value are swapped. Then the selection sort algorithm looks for the smallest item starting at the third location in the sequence. This pattern repeats until all the items in the sequence have been sorted. TheselSortfunction does the actual sorting for the algorithm.
4.5.2 The Selection Sort Code
1 def selSort(seq):
2 for i in range(len(seq)-1):
3 minIndex = select(seq, i)
4 tmp = seq[i]
5 seq[i] = seq[minIndex]
6 seq[minIndex] = tmp
We can visualize the selection sort algorithm by running an animation of it sorting.
The animation is pictured in Fig.4.3having sorted more than half the values in a sequence. The green dots represent items that are now in their proper location in the sequence. The height of the dot from the x-axis (i.e. the y-value) is its value. The x-axis is the position in the list. In this animation all the values between 0 and 199 are being sorted into ascending order. The upper-right corner represents those values that have not yet been sorted. The algorithm starts looking for the next smallest value just to the right of the green diagonal line. It finds the minimum value (i.e. closest to the x-axis) by going through all the remaining unsorted dots. Once it finds the small, shortest dot, it swaps it with the left-most dot to put it into is sorted position in the list. The complete code for this animation is given in Sect.20.3and can be downloaded from the website accompanying this text. Try it out!
104 4 Sequences
Fig. 4.3 Selection Sort Snapshot
Consider sorting the list [5 8 2 6 9 1 0 7] as depicted in Fig.4.4. After each call of theselectfunction from theselSortfunction the next element of the list is placed in its final location. Sorting the list leads to the intermediate steps as shown. Each time theselectfunction is called the new smallest element is swapped with the first location in the rest of the list to move the next smallest element into its location within the sorted list.
To find each new smallest element we callselectwhich must run through the rest of the list looking for the minimum element. After each pass the list in Fig.4.4is one item closer to sorting the whole list. It turns out that this early attempt at writing a sorting algorithm is not that great. The complexity of this algorithm is O(n2) because each time through the for loop in theselSortfunction we call the selectfunction which has its own for loop. Thefor iloop is executedntimes and each time it is executed thefor jloop must go through one less item looking for the smallest value that is left to be sorted. So, the first time we execute the body of thefor jloopn−1 times, thenn−2 times the second time select is called, thenn−3 times and so on.
We have seen this pattern before. The sum of the first n integers has an n2term in its formula. Therefore, selection sort is O(n2). This means as we try to sort some larger lists the algorithm will really start to slow down. You should never use this algorithm for sorting. Even on small lists we can do much better.
Fig. 4.4 Selection Sort of a List