Selection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted portion of a list and moves it to the beginning, building up a sorted portion step by step. In previous articles, we’ve looked at what selection sort is, how it works, and even its pseudocode and implementations in various programming languages. Now, let’s make this algorithm even clearer by using visualization and animation.
Why Visualization and Animation are Helpful
Sorting algorithms can sometimes seem a bit abstract. It can be easier to understand these topics by seeing it in action. Visualizations helps us understand by:
- Showing Comparisons: You can actually see which elements are being compared at each step.
- Illustrating Swaps: The swapping of elements to their correct position becomes very clear.
- Visualizing Overall Flow: Animations show you, step by step, how the algorithm goes from an unsorted state to a fully sorted one.
Visualization of Various Steps in Selection Sort Algorithm
Let’s visualize how selection sort works by applying it to a small array of numbers: [5, 1, 4, 2, 8]
. Selection sort finds the minimum value from the unsorted portion and gradually builds a sorted list by putting the minimum value at the beginning of the unsorted portion.
Here’s how the algorithm sorts the list step by step:
Step 0: Initial State
Initially, the entire array [5, 1, 4, 2, 8]
is considered unsorted. We haven’t started building the sorted portion yet.
Step 1: Finding and Placing 1
- We look through the entire array
[5, 1, 4, 2, 8]
to find the smallest element. - We find that
1
is the smallest. - We swap
1
with the first element in the unsorted portion, which is5
. - Now, the sorted portion is
[1]
, and the unsorted portion is[5, 4, 2, 8]
.
Below is the visualization of all the substeps performed in this step (Click on forward/backward buttons to move through the substeps):
Step 2: Finding and Placing 2
- We look at the unsorted portion
[5, 4, 2, 8]
to find the smallest element. - We find that
2
is the smallest. - We swap
2
with the first element of the unsorted portion, which is5
. - Now, the sorted portion is
[1, 2]
, and the unsorted portion is[4, 5, 8]
.
Below is the visualization of all the substeps performed in this step (Click on forward/backward buttons to move through the substeps):
Step 3: Finding and Placing 4
- We look at the unsorted portion
[4, 5, 8]
to find the smallest element. 4
is already the smallest in this portion.- We swap
4
with itself (which has no effect). - Now, the sorted portion is
[1, 2, 4]
, and the unsorted portion is[5, 8]
.
Below is the visualization of all the substeps performed in this step (Click on forward/backward buttons to move through the substeps):
Step 4: Finding and Placing 5
- We look at the unsorted portion
[5, 8]
to find the smallest element. - We find that
5
is the smallest. - We swap
5
with itself (which has no effect). - Now, the sorted portion is
[1, 2, 4, 5]
, and the unsorted portion is[8]
.
Below is the visualization of all the substeps performed in this step (Click on forward/backward buttons to move through the substeps):
Step 5: Final Element 8
- Only one element,
8
, is left in the unsorted portion. - Since it’s the only element, it’s already in the correct position.
- The entire array
[1, 2, 4, 5, 8]
is now sorted.
Below is the visualization of all the substeps performed in this step (Click on forward/backward buttons to move through the substeps):
After these steps, the entire array [1, 2, 4, 5, 8]
is sorted.
In summary: Selection sort works by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning of the unsorted portion. This builds the sorted portion step by step until the entire array is sorted.
Step By Step Slide Show
Below is the step by step visualization of all the steps and substeps in selection sort algorithm:
What’s Next?
Now that you have visualized how selection sort works, you might want to:
- Learn about advantages and disadvantages of selection sort
- Study the time and space complexity analysis to understand performance characteristics.
- Learn about the pseudocode and implementation details
- Study practical implementations in different programming languages