Selection sort is small, relatively easy to understand, and works well for small arrays (a few tens of elements). These characteristics make it an excellent example of using arrays. However, the selection sort is sensitive to the data size (i.e., the array's size). In CS 2420, you will learn about run order and Big-O notation. Computer scientists use these tools to express the relative time an algorithm takes to solve a problem, expressing the runtime in terms of the data's size. Selection sort has a run order of O(n2), which means that doubling the data size quadruples the time needed to sort it. Faster sorting algorithms are desirable for processing larger amounts of data, and you will study more efficient algorithms, such as quicksort and merge sort, in CS 2420.
The selection sort algorithm is named for its basic behavior. It selects the smallest value in an array, swaps it with the element at the top, effectively shrinks the array by one element, and then repeats the process until the array size is 0 and the array is sorted. (We can modify the algorithm to select the largest element and swap it with the element at the bottom of the array.) Selection sort doesn't change the data array size; instead, it changes the value stored in a variable, top, that indexes into the array. For the rest of this discussion, we'll use the term sub-array to help clarify this organization.
Implementing selection sort requires two loops, one nested inside the other. The outer loop selects the smallest value and swaps it with the top element. The outer loop also "shrinks" the sub-array by advancing the top index variable. A for-loop, for (int top = 0; top < size; top++), conveniently carries out these steps. The inner loop performs the selection task by identifying the smallest element in the sub-array, which it does by comparing the element at the top with the remaining sub-array elements. When dealing with simple or primitive data, the "less-than" operator is sufficient for comparing and ordering the array elements. More complex data requires creating an appropriate ordering function, which is discussed in the next chapter. Figure 1 illustrates the swapping task.
(a)
(b)
(c)
(d)
Swapping the smallest and top elements. The green box represents the element stored at the top of the current sub-array and the blue box represents the smallest remaining element.
The initial state of the array. The variables top and min are indexes in the data array. The sub-array is that part of the data array that lies between the top index and the bottom of the data array. The min index is the smallest element in the sub-array, as identified by the selection task.
Swapping the smallest and the top elements takes three steps. The first step copies or saves the smallest value in the sub-array into a temporary variable.
In a sense, saving the smallest array element "frees" that location in the array. The second step copies or saves the value at the top of the array into this now "free" or "empty" location.
The final step copies the value stored in the temporary variable to the top of the sub-array.
Simple Data
The main function is a simple driver that tests the selection sort function. The following examples present three slightly different versions of the selection sort algorithm. The first version is a single function; the second and third versions define a swap function that swaps array elements. Other than the function prototypes appearing at the top, the main functions are the same for all three versions.