Selection sort is small, relatively easy to understand and implement, and works well for small arrays (a few tens of elements). These characteristics make it an excellent example of array use, but it is sensitive to data size. Computer scientists use Big O notation to express an algorithm's general runtime as a function of data size. Using this notation, Selection sort's runtime is O(n2), which means that doubling the data size quadruples the time needed to sort it. Faster sorting algorithms, such as quicksort, are desirable for processing larger data sets.
The selection sort selects the smallest value in an array, swaps it with the element at the top, and logically moves the top down by one element. It repeats this cycle until the array size is 0 and the array is sorted. The algorithm can also select the largest element and swap it with the bottom element. Selection sort doesn't physically change the array size; instead, it maintains an index to the top (or bottom) that divides the array into two subarrays: sorted and unsorted. For simplicity, the following descriptions refer to the unsorted subarray as "the" subarray.
The selection sort requires two nested loops.
|
for (int top = 0; top < size; top++)
{
int min = top;
for(int i = top + 1; i < size; i++)
if (data[i] < data[min])
min = i;
// swap min and top
} |
![]() |
![]() |
![]() |
![]() |
| (a) | (b) | (c) | (d) |
sizeof, pass by pointer, pass by referenceThe following functions demonstrate the selection sort with an array of characters, but the syntax works with any fundamental type (e.g., int or double),
| Driver | Selection Sort |
|---|---|
#include <iostream>
using namespace std;
void ssort(char* data, int size);
int main()
{
char data[] = { 'S','E','E','T','H','E','Q','U',
'I', 'C','K','R','E','D','F','O','X' };
|
|
The outer for-loop manages the subarray, applying the selection operation, swapping array elements, and shrinking the subarray with each iteration. The inner loop initially selects the top subarray element and compares it to the remaining elements, saving or selecting the index location of the smallest.
| Pass By Pointer | Pass By Reference |
|---|---|
void swap(char |
void swap(char |
Contemporary compilers often simplify pass by pointer by automatically typecasting function arguments to a pointer, making the address-of operator unnecessary. Consequently, programmers can simplify the pass-by-pointer function call in the first example to
swap(data[min], data[top]);
sizeof, struct, pass by pointer, pass by reference| student.h | client |
|---|---|
struct student
{
const char* name;
int id;
double gpa;
};
void ssort(student* data, int size); |
#include <iostream>
#include <iomanip>
#include "student.h"
using namespace std;
int main()
{
student students[] = {
{ "Dilbert", 123, 3.5 },
{ "Wally", 456, 2.0 },
{ "Alice", 987, 3.9 },
{ "Asok", 730, 3.8 },
{ "Catbert", 501, 3.0 },
{ "Pointy Haired Boss", 666, 1.0 },
{ "Dogbert", 111, 4.0 }
};
int size = sizeof(students) / sizeof(student);
|
| ssort.cpp | |
#include "student.h" |
if (strcmp(data[i].name, data[min].name) < 0)
| Pass By Pointer | Pass By Reference | Pass The Data Array |
|---|---|---|
void ssort(student* data, int size)
{
. . . .
swap(
void swap(student |
void ssort(student* data, int size)
{
. . . .
swap(data[top], data[min]);
}
void swap(student |
void ssort(student* data, int size)
{
. . . .
swap(
void swap( |
The last example demonstrates another option: passing the data array to the swap function. Whereas the other examples pass two array elements to the swap function, this version passes the entire array and two array indexes. Although the program passes the entire array, the function doesn't need the array's size and assumes that the two index values are within bounds.