Insertion Sort

Introduction

Insertion sort builds a sorted region at the front of an array one element at a time. On each pass it takes the next unsorted element, compares it backward against the already-sorted portion, shifts larger elements one position to the right, and drops the element into the gap. The mental model most people use is sorting a hand of playing cards: you pick up one card at a time and slide it into its correct place among the cards you already hold.

In this course, insertion sort matters for two reasons. First, it is simple enough that you can trace every comparison and shift by hand, which makes it an ideal vehicle for learning how to count operations and derive Big O bounds. Second, its performance profile is genuinely useful: it runs in linear time on nearly-sorted input, sorts in place with O(1) extra memory, and is stable, which is why real-world hybrid sorts fall back to it for small subarrays.

The videos below are taught in C++ and Java, but the algorithm is language-independent — the loop structure translates directly to JavaScript, so watch the logic and then write your own JS version. As you watch, pay attention to three things: the role of the inner while loop (shifting versus swapping), why the comparison uses a strict greater-than (this is what preserves stability), and how the number of shifts changes between already-sorted, random, and reverse-sorted input. Those observations are exactly what you need for the complexity and comparison subtopics.

Video 1: Insertion Sort Algorithm | C++ / Java Complete explanation for Beginners and Code | DSAOne Course 18

Anuj Kumar Sharma · 17:24 · 207,951 views · 5y ago · captions — Watch on YouTube

About this video

A beginner-oriented walkthrough of the insertion sort algorithm from the DSAOne course series, covering the core idea and a complete code implementation in C++ and Java. At roughly 17 minutes it is the longest of the three, so it has room to explain the reasoning behind each loop before showing the code. Expect a step-by-step trace of how elements are shifted into position followed by a line-by-line coding segment.

Why this video

Its length and explicit beginner framing make it a strong first watch for the "How Insertion Sort Works" and "Step-by-Step Example" subtopics, and the complete code walkthrough gives you a reference structure to port into JavaScript.

Video 2: Insertion Sort in Java - Part 1 | Animation | Sorting Algorithms

Dinesh Varyani · 13:26 · 17,807 views · 5y ago · captions — Watch on YouTube

About this video

Part 1 of Dinesh Varyani's insertion sort coverage, built around animated slides that visualize the algorithm as it progresses through an array. The animation focus means you should see the sorted and unsorted regions, the element being inserted, and the backward shifting made visible rather than just described. The implementation shown is in Java.

Why this video

Animation is particularly effective for insertion sort because the shifting behavior is hard to follow from code alone; this video directly supports the step-by-step example subtopic and builds the intuition you need for the complexity analysis.

Video 3: Insertion Sort Explained and Implemented with Examples in Java | Sorting Algorithms | Geekific

Geekific · 11:21 · 5,039 views · 5y ago · captions — Watch on YouTube

About this video

Geekific's concise explanation of insertion sort, combining the conceptual explanation with a worked example and a Java implementation, with source code available on the channel's GitHub repository. At about 11 minutes it is the most compact treatment of the three and is part of a broader sorting algorithms series. Videos in this series typically close by noting the algorithm's complexity and properties.

Why this video

It serves well as a review or second pass after a longer video, and its placement within a full sorting-algorithms series makes it useful for seeing where insertion sort sits relative to the other algorithms in this module.

How These Videos Support the Learning Outcomes

Together these videos give you the algorithmic logic and concrete code structure needed for MO1 — once you can follow the outer loop, the key variable, and the inner shifting loop, translating insertion sort into JavaScript is straightforward. The traced examples and animations let you count comparisons and shifts on sorted, random, and reverse-sorted input, which is the evidence base for the best-, average-, and worst-case Big O analysis in MO2 and for recognizing that the algorithm uses only constant extra space. Observing that equal elements are never swapped past one another, and that all work happens within the original array, supports the stability and in-place discussion in MO3. Finally, seeing insertion sort alongside the other algorithms in these channels' sorting series helps you reason about when its near-linear behavior on nearly-sorted or small data makes it the right choice, which is the judgment MO4 asks for.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.