Merge Sort

Introduction

Merge sort is the classic example of divide-and-conquer: split an array in half, sort each half recursively, then merge the two sorted halves back into one sorted array. The real work happens not in the splitting but in the merge step, where two already-sorted sequences are combined in linear time by repeatedly taking the smaller of the two front elements. Because the array is halved at every level of recursion and each level does O(n) merging work, merge sort runs in O(n log n) time on every input — best, average, and worst case alike.

That consistency is why merge sort matters in this course. Unlike bubble sort or insertion sort, it does not degrade on reversed or random data, and unlike quicksort it has no pathological worst case. The trade-off is memory: a straightforward implementation allocates auxiliary arrays, giving O(n) auxiliary space rather than quicksort's in-place behavior. Merge sort is also stable, which matters when you sort records by one key after another.

As you watch these videos, trace the recursion carefully: note the base case (an array of length 0 or 1 is already sorted), the point where recursion stops and merging begins, and the order in which merges actually execute. Pay particular attention to the merge helper — the two index pointers, the comparison, and the cleanup loops that copy any leftover elements. The videos below use C++ and Java, so translate the logic into JavaScript as you go; the control flow is identical, and array slicing in JavaScript often makes the code shorter than the examples shown.

Video 1: L-3.3: How Merge Sort Works?? Full explanation with example

Gate Smashers · 9:52 · 1,582,402 views · 4y ago · captions — Watch on YouTube

About this video

A concise lecture walking through how merge sort works step by step on a worked example, showing the recursive division of the array and the merging back together. It focuses on building intuition for the divide-and-conquer strategy rather than on code.

Why this video

Gate Smashers is a widely followed exam-prep channel known for clear blackboard-style explanations, and this short video is the fastest way to internalize the recursive structure and the divide-and-conquer idea before you write any code.

Video 2: Merge Sort Algorithm | C++ / Java Complete explanation for Beginners and Code | DSA-One Course #21

Anuj Kumar Sharma · 19:51 · 263,951 views · 5y ago · captions — Watch on YouTube

About this video

Part of the DSA-One course, this longer video covers the concepts behind merge sort and then develops a complete implementation in C++ and Java, including the merge helper function. It is paced for beginners and works through the code line by line.

Why this video

The extended runtime allows the instructor to connect the conceptual picture to actual code, which is exactly the bridge you need before writing your own JavaScript version with its recursive calls and merge routine.

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

Geekific · 15:07 · 8,589 views · 5y ago · captions — Watch on YouTube

About this video

Geekific explains merge sort with examples and then implements it in Java, situating it alongside the channel's other sorting algorithm videos. The description points to a public GitHub repository where the code can be read and compared.

Why this video

This video reinforces the implementation from a second perspective and places merge sort in the context of other sorting algorithms, supporting the comparison work on time complexity, space usage, and practical trade-offs.

How These Videos Support the Learning Outcomes

Together these videos move from intuition to implementation to comparison. The Gate Smashers lecture establishes the divide-and-conquer strategy and recursive structure you will mirror when coding merge sort in JavaScript (MO1), while the Anuj Kumar Sharma and Geekific videos show full implementations, including the merge helper, that you can translate line by line. All three address why the recursion depth of log n combined with linear merging yields O(n log n) in every case, and the implementations make the auxiliary array usage visible so you can reason about O(n) space (MO2). Seeing merge sort coded next to other sorting algorithms supports the stability and in-place comparisons (MO3) and gives you the evidence needed to argue when merge sort is the right choice versus quicksort or insertion sort for a given data size and memory budget (MO4).

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.