This is my second post on Sorting algorithms.
We will see how Insertion Sort works
Insertion sort is another naive sorting approach which can't be used for large data sets. But, if you understand the crux of how it works under the hood. It's easy to implement.
Below is the Pseudocode for the sorting algorithm
- Element at the very first index is assumed sorted
- You pick the element at the second index
- Compare the element with the sorted subarray list (one element at a time)
- If element compared within the sorted subarray list is bigger than the current you will shift them one place to the right.
- You will repeat the above process until either you reach the left end of the array or if the element compared with is less than the current element.
- Repeat the process until all elements are sorted.
I got this beautiful visual representation of how it works in a nutshell.
Please click on the below reference link to check it out.
This is not a pure function-based implementation!
Hope you like this, please share your comments and I am looking forward to any optimizations that can be done here.
On my next post, we will see the Selection Sort and also understand how it's similar to the Insertion sort we saw above.
For this content, I got an awesome video reference that I can't wait to share.
See you in the next post!