This shows the process for sorting an array of five elements using the selection sort method:
The array of numbers are:
A selection sort behaves in the following way:
Pass One:
During pass one, we are going to find the LOWEST number and have it switch places with the first element in the array. Now, here's how we start looking:
The green arrow is pointing at the second number in the list. For now, we're going to assume that the first element is the lowest in the list.
Now, 5 is less than 21, so we make the black arrow point to the number 5 - to keep track of the lowest number we have so far.
The green arrow, now points at 2. 2 is less than 5, so again we move the black pointer and have it point at 2 to keep track of it as the lowest number.
Now, 16 is being compared with the number 2. 16 is not less than 2, so the black arrow doesn't change positions, only the green one does. Keep in mind that the green arrow is the number we are comparing with to see if it is lower than the one pointed to by the black arrow.
The green arrow now points at 8, which is still not less than 2. Now, we have determined that the number 2 is the lowest number in the list - which the black arrow is pointing at. What we do is have the number 2 swap places with the value of the first element in the list - 21, like so:
![]() |
| The value of the third element swaps places with the value of the first element. |
This completes pass one.
Pass Two:
On pass two, we have already found the smallest number in the list and we've moved it to the beginning of the list. Since we now know that 2 is at the start of the list, we can begin checking the elements starting from the second element in the list; we make the black arrow point to the second element in the list.
Notice that on Pass Two, we start from the second element in the list. On pass one, we were finding out what the lowest value in the list was. On this second pass, we are going to find out the second-lowest value in the list. For now, we have the black arrow pointing at the number 5, and we are going to compare it with all the other numbers in the list. As you know by now the green arrow will point to each number in turn to see which is less than 5. Since none of the numbers are less than 5, no swap is needed - nothing happens.
This ends Pass Two.
Pass Three:
![]() |
| On the third pass, we start at the third element. |
For now, we again assume that the number in the third position (21) is the lowest value we have so far. The green arrow points to the next value, 16, and we compare it with 21. Now, 16 is lower so we have the black arrow point at it, to "keep track" of the lowest value we've found so far.
This brings us to:
Now, we compare 8 with 16. Of course, 8 is lesser, so we have the black arrow pointing at the lowest element we've found.
At the end of this THIRD pass, we will put the number 8, which is the third-lowest value into the third element of the array:
![]() |
| Pass Three: Third Lowest Element in the entire list is found. |
This ends Pass Three.
Pass Four:
Now, we compare 21 with 16. No swap is needed. The array is now sorted. This ends pass four and also, the entire sorting process. Keep in mind that even though we had 5 elements to sort, we only needed 4 passes. This holds true for ANY array size. If you have 100 elements to sort, you will only need 99 passes to fully sort the array.
The C++ code is shown below - the variable names are blackArrow and greenArrow, so that it will be easier to follow the code using the explanation I've written above.
int array[] = { 21, 5, 2, 16, 8 };
int blackArrow = 0; //Will hold the index of the lowest value in the list each pass
int greenArrow = 0; //Will hold the index of the element we are comparing with the black arrow
for (int pass = 0; pass < 4; pass++)
{
blackArrow = pass;
for (int greenArrow = blackArrow + 1; greenArrow < 5; greenArrow++)
{
if (array[greenArrow] < array[blackArrow])
{
blackArrow = greenArrow;
}
}
//If pass is 0, then the lowest value found will be swapped with the value in array[0].
//If pass is 1, then the second-lowest value found will be swapped with the value in array[1].
//If pass is 2, then the third-lowest value found will be swapped with the value in the third position - array[2].
//If pass is 3, then the fourth-lowest value found will switch places with the value in the array's fourth position.
int temp = array[pass];
array[pass] = array[blackArrow];
array[blackArrow] = temp;
}
int blackArrow = 0; //Will hold the index of the lowest value in the list each pass
int greenArrow = 0; //Will hold the index of the element we are comparing with the black arrow
for (int pass = 0; pass < 4; pass++)
{
blackArrow = pass;
for (int greenArrow = blackArrow + 1; greenArrow < 5; greenArrow++)
{
if (array[greenArrow] < array[blackArrow])
{
blackArrow = greenArrow;
}
}
//If pass is 0, then the lowest value found will be swapped with the value in array[0].
//If pass is 1, then the second-lowest value found will be swapped with the value in array[1].
//If pass is 2, then the third-lowest value found will be swapped with the value in the third position - array[2].
//If pass is 3, then the fourth-lowest value found will switch places with the value in the array's fourth position.
int temp = array[pass];
array[pass] = array[blackArrow];
array[blackArrow] = temp;
}
That's it, folks! I hope that helps the beginner understand the selection sort - even just a little bit.
If you have any questions, email me at: sparkprogrammer@gmail.com
-Joe
-Joe












