Selection Sort Algorithm Visually Explained

Visually Explained Algorithms

The Selection Sort algorithm sorts an array by looking for the smallest item and moving it to the front of the list. That’s really all you have to know. Let’s jump into an example.

Selection Sort starts by setting the first value as the minimum value.

It then starts comparing it with each value in the array. If a lower value is found, the minimum value is updated, and the traversal continues until the end of the array is reached. During the first comparison, the value at index 1 is compared to the minimum value.

Since the value is smaller than the minimum value, the minimum value is updated, and the index is incremented. Next, 2 is compared to the minimum value. Since 2 is not less than the minimum value, the minimum value stays the same, and the index is incremented.

Next, 7 is compared to 1. Number 1 is still smaller.

Selection Sort compares 1 and 4. Since 1 is smaller, the minimum value remains.

The algorithm has reached the end of the array for the first traversal. The first element is swapped with the element that contains the minimum value. In this case, 5 is swapped with 1. The first element is marked as sorted.

The algorithm starts the second traversal from the element at index 1. The value at element 1 is set as the minimum value.

The first comparison of the second iteration is between 5 and 2. Since 2 is smaller than the minimum value, the minimum value is updated to 2.

Next, 2 is compared to 7. Since 7 is larger than 2, the minimum value stays the same.

The algorithm has reached the end of the array for the second traversal. The second element is swapped with the element that contains the minimum value. In this case, 5 is swapped with 2. The second element is marked as sorted.

The algorithm starts with the third iteration. It sets the minimum value as the first unsorted element in the array. The value at element 2 is set as the minimum value.

The Selection Sort algorithm starts doing the third traversal. The first comparison is between 5 and 7. Since 5 is smaller than 7, the algorithm keeps 5 as the minimum value.

The index is incremented and 4 is compared to the minimum value. Since 4 is smaller than the minimum value, the minimum value is updated to 4.

The fourth iteration starts by setting the element at index 3 as the minimum value.

A comparison is made between 7 and 5. Since 5 is less than 7, the minimum value is updated to 5.

The Selection Sort algorithm has reached the end of the array. It swaps the element at index 3 with the minimum value. The first four elements are sorted.

In the next traversal, there is only one element left. Since there’s only one element left, there is no need to compare it to anything else. The Selection Sort algorithm marks the last element as sorted and completes the sorting procedure.

If you liked what you read, my book, An Illustrative Introduction to Algorithms, covers this Algorithm and many more.

 

Leave a Reply