Logo

dev-resources.site

for different kinds of informations.

Understanding insertion sort algorithm

Published at
6/5/2023
Categories
javascript
algorithms
sorting
tutorial
Author
marcossnikel
Author
12 person written this
marcossnikel
open
Understanding insertion sort algorithm

In our day-to-day JavaScript programming, when we use the .sort() method, there's a whole algorithm running behind the scenes. However, it's unlikely that Insertion Sort is the algorithm being used. But why is that?

  • There are various sorting algorithms available, such as Insertion Sort, Quick Sort, Merge Sort, Selection Sort, Bubble Sort, and many others. Each of them has different time and space complexities.

Insertion Sort happens to be one of the less efficient ones, but why?

In terms of complexity, Insertion Sort has a time complexity of O(nยฒ) in most cases and a space complexity of O(1), which is good. However, considering a time complexity of this magnitude, there are sorting algorithms that achieve a time complexity of O(n log n), which is better than O(nยฒ).

So, how does this algorithm work?

  1. Initially, we consider the first element of the list as a sorted list with a single element.

  2. Next, we move on to the next element in the unsorted list.

  3. We compare the current element with the elements in the sorted list, from right to left, until we find the correct position to insert the element.

  4. During the comparison, if an element in the sorted list is greater than the current element, it is shifted one position to the right to make space for the insertion.

  5. We repeat steps 3 and 4 until all elements in the unsorted list are inserted into the sorted part of the list.

  • This process continues until all elements are in the sorted list, resulting in a completely ordered list. Insertion Sort is an efficient sorting algorithm for small lists or partially sorted lists. However, its performance significantly deteriorates for very large lists.

Below, I provide an example implementation of this algorithm, including an auxiliary function called swap() responsible for swapping elements.

Code example

sorting Article's
30 articles in total
Favicon
Difference Between Merge Sort and Quick Sort
Favicon
Leetcode 75. Sort Colors
Favicon
Sorted Data Structures in Python
Favicon
Sorting Algorithms That Use Hash Tables
Favicon
C# Essentials: Operator Overloading and Custom Sorting Made Simple
Favicon
Recap the highlight of the sorting algorithms using JavaScript for beginners
Favicon
Merge Sort Demystified: A Beginner's Guide to Divide and Conquer Sorting
Favicon
Understanding Bubble Sort: Simple Sorting Method
Favicon
Introduction to Sorting Algorithms in JavaScript
Favicon
Understanding the SQL ORDER BY Clause
Favicon
Demystifying Sorting Algorithms: Making Order Out of Chaos
Favicon
Merge Intervals : A unique Graph-based approach
Favicon
Bubble Sort
Favicon
COMPARATOR vs COMPARABLEโ€Š-โ€ŠA Java Surprise You did inย School!
Favicon
Streamlining Data Management with Python's sorted() Function
Favicon
1 billion rows challenge in MySQL
Favicon
1 billion rows challenge in PostgreSQL and ClickHouse
Favicon
Sorting in Java โ€“ how to, and how not to do it anymore
Favicon
Reversing sort order in Rust
Favicon
Priority Queue: Creating order from chaos
Favicon
Mastering Array Sorting in PHP: usort & uasort ๐Ÿš€๐Ÿš€
Favicon
QuickSort - Time Analysis (Part2)
Favicon
Quicksort (Grokking Algorithms)
Favicon
Better Bogo Sort
Favicon
Sorting Array of Objects in Javascript
Favicon
Bubble Sort
Favicon
Understanding insertion sort algorithm
Favicon
Sorting Visualizer [ A web app to visualize sorting algorithm ]
Favicon
How to sort complex objects with custom criteria in Python
Favicon
Iterative Sorting algorithms in Javascript

Featured ones: