JavaScript Algorithms: What Is Insertion Sort, A Detailed Step-By-Step, And Example Code

By: Jeff Lewis

Github:

1. What Is Insertion Sort?

A. Insertion Sort Definition:

Insertion Sort is a Sorting Algorithm, which iterates over the array and builds the sorted array one element at a time. As it iterates over the array, it selects an element (Element #1), and iterates again. As the second loop iterates, it’s comparing each proceeding value (Element #2) to the selected element (Element #1) in the first iteration.

If Element #2 is less than Element #1, we iterate backwards until we find an index where Element #2 is not less that Element #1. When we reach the right index, we “insert” Element #2.

Insertion Sort is known to be one of the slower sorting algorithms.

B. Insertion Sort Gif:

C. Insertion Sort Step-By-Step Process Breakdown:

In this step-by-step process, we’re going to be breaking down how to do a Insertion Sort on the following “exampleArray.” This is the same array from the gif in Step B.

let exampleArray = [6, 5, 3, 1, 8, 7, 2, 4];

We are first going to iterate over the array using a For Loop using “i” to keep track of the index. Within the first For Loop, we are going to need a selectedElement variable to mark the spot between the sorted section and the unsorted section. Additionally, we are going to need to declare a comparisonIndex variable to keep track of which index value we are comparing the selectedElement to. Now we can start the second iteration. The comparisonIndex will start one index before the selectedElement , which it will iterate backwards if the value at the comparisonIndex is greater than the selectedElement AND greater than 0 (The beginning of the array). As we iterate backwards and swapping numbers when the value at the comparsionIndex is greater than the selectedElement , we will reach a point where it is not. Once we reach that point, we will insert the selectedElement in to the the correct index. We will repeat this process until the first iteration is finished with iterating over the entire array.

2. How Do We Use Insertion Sort?

A. Insertion Sort Code:

The code provided below shows 2 versions:

With Comments:

// Insertion Sort

const insertionSort = (array) => {

// Iterate Over Array (Starts At 1 Because Nothing Left Of Index 0)

for (let i = 1; i < array.length; i++) {

// Selected Element + Comparison Index

let selectedElement = array[i];

let comparisonIndex = null;

console.log(`Selected Element: ${selectedElement}`); // Iterate Over Array Backwards (Comparison Index Must Be Greater Or Equal To 0 And Greater Than The Selected Element)

for (comparisonIndex = i - 1; comparisonIndex >= 0 && array[comparisonIndex] > selectedElement; comparisonIndex--) {

console.log(`Comparison Index: ${comparisonIndex}`); // Swap Index Values

console.log(`Swapping ${array[comparisonIndex + 1]} for ${array[comparisonIndex]}`);

array[comparisonIndex + 1] = array[comparisonIndex];

} // Insert Selected Element

console.log(`Selected Element (${selectedElement}) inserted at index ${comparisonIndex + 1}`);

array[comparisonIndex + 1] = selectedElement;

} // Return Array

return array;

};

2. Without Comments: