Insertion sort is an algorithm used to sort a list of items in ascending, descending or any other custom order. In the previous articles, we’ve explored what insertion sort is, how it works, and even looked at its pseudocode. Now, let’s bring this algorithm to life through visualization and animation. By the end of this article, you’ll gain an intuitive understanding of how insertion sort modifies data, making the entire concept much clearer and easier to grasp.
Why Visualization and Animation are Helpful
Sorting algorithms can sometimes feel abstract. Instead of just reading about how they work, seeing them in action can make a huge difference! Visualizations help us understand:
- How elements are compared: You can actually see which elements are being compared at each step.
- How elements are shifted: The shifting of elements to create space for insertion becomes very clear.
- The overall flow of the algorithm: Animation provides a step-by-step view of how the algorithm progresses from an unsorted to a sorted state.
Visualization of Various Steps in Insertion Sort Algorithm
Let’s visualize how insertion sort works by applying it to a small array of numbers: [8, 2, 4, 1, 3]
. Insertion sort visits one element at a time from the unsorted portion and gradually builds a sorted list by “inserting” elements into their correct positions within the sorted portion.
Here’s how the algorithm sorts the list step by step:
Step 0: Initial State
Initially, we consider the first element 8
to be already sorted and this becomes the initial sorted portion. The rest of the array [2, 4, 1, 3]
is the unsorted portion that we’ll gradually insert into the sorted portion.
Step 1: Inserting 2
- We pick the first element from the unsorted portion, which is
2
. - We compare
2
with8
(the last element in the sorted portion). - Since
2
is less than8
, we shift8
to the right to make space for2
. - We insert
2
into its correct position at the beginning of the array. - Now, the sorted portion is
[2, 8]
and the unsorted portion is[4, 1, 3]
.
Step 2: Inserting 4
- We pick the first element from the unsorted portion, which is
4
. - We compare
4
with8
(the last element in the sorted portion). - Since
4
is less than8
, we shift8
to the right. - We compare
4
with2
(the next element from the end). - Since
4
is greater than2
, we insert4
between2
and8
. - Now, the sorted portion is
[2, 4, 8]
and the unsorted portion is[1, 3]
.
Step 3: Inserting 1
- We pick the first element from the unsorted portion, which is
1
. - We compare
1
with8
(the last element in the sorted portion). - Since
1
is less than8
, we shift8
to the right. - We compare
1
with4
, since1
is less than4
, we shift4
to the right. - We compare
1
with2
, since1
is less than2
, we shift2
to the right. - We insert
1
at the beginning of the array. - Now, the sorted portion is
[1, 2, 4, 8]
and the unsorted portion is[3]
.
Step 4: Inserting 3
- We pick the first element from the unsorted portion, which is
3
. - We compare
3
with8
(the last element in the sorted portion). - Since
3
is less than8
, we shift8
to the right. - We compare
3
with4
, since3
is less than4
, we shift4
to the right. - We compare
3
with2
, since3
is greater than2
, we insert3
between2
and4
. - Now, the sorted portion is
[1, 2, 3, 4, 8]
and the unsorted portion is empty.
After these steps, the entire array [1, 2, 3, 4, 8]
is sorted.
In summary: Insertion sort works by repeatedly taking an element from the unsorted portion and inserting it into the correct position within the sorted portion. This process continues until the entire array is sorted.
Step By Step Slide Show
Below is the step by step visualization of insertion sort algorithm:
What’s Next?
Now that you have visualized how insertion sort works, you might want to:
- Learn about advantages and disadvantages of insertion sort
- Compare it with other sorting algorithms to know when to use insertion sort.
- Study the time and space complexity analysis to understand performance characteristics.