1.1 Sorting an Array: Selection Sort
S orting is the process by which a set of values are arranged in ascending or descending order. There are many reasons to sort. Sometimes we sort in order to produce more readable output (for example, to produce an alphabetical listing). A teacher may need to sort her students in order by name or by average score. If we have a large set of values and we want to identify duplicates, we can do so by sorting; the repeated values will come together in the sorted list.
Another advantage of sorting is that some operations can be performed faster and more efficiently with sorted data. For example, if data is sorted, it is possible to search it using binary searchthis is much faster than using a sequential search. Also, merging two separate lists of items can be done much faster than if the lists were unsorted.
There are many ways to sort. In this chapter, we will discuss two of the simple methods: selection and insertion sort. In , we will look at more sophisticated ways to sort. We start with selection sort.
Consider the following list of numbers stored in a C array, num :
Sorting num in ascending order using selection sort proceeds as follows:
st pass
Find the smallest number in the entire list, from positions to ; the smallest is , found in position .
Interchange the numbers in positions and . This gives us the following:
nd pass
Find the smallest number in positions to ; the smallest is , found in position .
Interchange the numbers in positions and . This gives us the following:
rd pass
Find the smallest number in positions to ; the smallest is , found in position .
Interchange the numbers in positions and . This gives us the following:
th pass
Find the smallest number in positions to ; the smallest is , found in position .
Interchange the numbers in positions and . This gives us the following:
th pass
Find the smallest number in positions to ; the smallest is , found in position .
Interchange the numbers in positions and . This gives us the following:
th pass
Find the smallest number in positions to ; the smallest is , found in position .
Interchange the numbers in positions and . This gives us the following:
The array is now completely sorted. Note that once the 6th largest (65) has been placed in its final position (5), the largest (79) would automatically be in the last position (6).
In this example, we made six passes. We will count these passes by letting the variable h go from to . On each pass, we find the smallest number from positions h to . If the smallest number is in position s , we interchange the numbers in positions h and s .
In general, for an array of size n , we make n-1 passes. In our example, we sorted seven numbers in six passes. The following is a pseudocode outline of the algorithm for sorting num[0..n-1] :
for h = 0 to n - 2
s = position of smallest number from num[h] to num[n-1]
swap num[h] and num[s]
endfor
We can implement this algorithm as follows, using the generic parameter, list :
void selectionSort(int list[], int lo, int hi) {
//sort list[lo] to list[hi] in ascending order
int getSmallest(int[], int, int);
void swap(int[], int, int);
for (int h = lo; h < hi; h++) {
int s = getSmallest(list, h, hi);
swap(list, h, s);
}
}
The two statements in the for loop could be replaced by this:
swap(list, h, getSmallest(list, h, hi));
We can write getSmallest and swap as follows:
int getSmallest(int list[], int lo, int hi) {
//return location of smallest from list[lo..hi]
int small = lo;
for (int h = lo + 1; h <= hi; h++)
if (list[h] < list[small]) small = h;
return small;
}
void swap(int list[], int i, int j) {
//swap elements list[i] and list[j]
int hold = list[i];
list[i] = list[j];
list[j] = hold;
}
To test whether selectionSort works properly, we write Program P1.1. Only main is shown. To complete the program, just add selectionSort , getSmallest , and swap .
Program P1.1
#include
#define MaxNumbers 10
int main() {
void selectionSort(int [], int, int);
int num[MaxNumbers];
printf("Type up to %d numbers followed by 0\n", MaxNumbers);
int n = 0, v;
scanf("%d", &v);
while (v != 0 && n < MaxNumbers) {
num[n++] = v;
scanf("%d", &v);
}
if (v != 0) {
printf("More than %d numbers entered\n", MaxNumbers);
printf("First %d used\n", MaxNumbers);
}
//n numbers are stored from num[0] to num[n-1]
selectionSort(num, 0, n-1);
printf("\nThe sorted numbers are\n");
for (int h = 0; h < n; h++) printf("%d ", num[h]);
printf("\n");
}
The program requests up to ten numbers (as defined by MaxNumbers ), stores them in the array num , calls selectionSort , and then prints the sorted list.