MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Merge Sort: Divide-and-Conquer Paradigm & Stability Analysis0%
Study Materials
Data Structures & AlgorithmsProgress 0%
1.1Introduction to DSA, Big-O, Big-Omega & Big-Theta Notations1.2Time Complexity Calculation Rules & Common Runtime Patterns1.3Space Complexity, Auxiliary Memory & Stack vs Heap Allocation
6.1Binary Search: Search Space Elimination, Lower Bound & Upper Bound6.2Binary Search on Answer Space: Allocation & Feasibility Checks6.3Elementary Sorting Algorithms: Bubble, Selection & Insertion Sort (O(n²))6.4Merge Sort: Divide-and-Conquer Paradigm & Stability Analysis6.5Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies
HomeTutorialsData Structures & AlgorithmsMerge Sort: Divide-and-Conquer Paradigm & Stability Analysis
Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies

Merge Sort: Divide-and-Conquer Paradigm & Stability Analysis

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Merge Sort is an optimal, comparison-based sorting algorithm based on the Divide-and-Conquer design paradigm. It provides a guaranteed worst-case, average-case, and best-case time complexity of $O(n \log n)$ while maintaining strict stability.


1. Real-World Analogy: Sorting Two Piles of Graded Exam Papers

Imagine two teaching assistants have each sorted a stack of 50 student exam papers alphabetically:

  • Stack A: 50 sorted papers (Aarav, Ananya, David ...)
  • Stack B: 50 sorted papers (Ben, Chris, Emily ...)
  • To merge them into a single 100-paper sorted stack, you don't start from scratch!
  • You look at the top sheet of Stack A and top sheet of Stack B: compare Aarav with Ben. Aarav comes first → place in final pile.
  • Repeat. In a single pass of 100 quick comparisons, the entire stack is perfectly merged!

2. The Divide-and-Conquer Triad

  1. 1
    Divide: Split the array at midpoint $\text{mid} = \frac{\text{low} + \text{high}}{2}$ into two equal halves until subarrays contain $\le 1$ element (the base case).
  2. 2
    Conquer: Recursively sort both the left half and right half.
  3. 3
    Combine (Merge): Merge the two sorted subarrays back together into a single sorted array.
1Recursion Tree for Merge Sort on [38, 27, 43, 3, 9, 82, 10]:
2Level 0: [38, 27, 43, 3, 9, 82, 10]
3Level 1: [38, 27, 43] [3, 9, 82, 10]
4Level 2: [38] [27, 43] [3, 9] [82, 10]
5Level 3: [38] [27] [43] [3] [9] [82] [10]
6------------------------ MERGING PHASE ------------------------
7Level 2: [38] [27, 43] [3, 9] [10, 82]
8Level 1: [27, 38, 43] [3, 9, 10, 82]
9Level 0: [3, 9, 10, 27, 38, 43, 82] (SORTED!)

3. The Recurrence Relation & Master Theorem Proof

$$\text{Time Complexity: } T(n) = 2 T\left(\frac{n}{2}\right) + O(n)$$

  • The tree has $\log_2(n)$ levels.
  • At each level, merging all elements takes total $O(n)$ work.
  • Total Time: $\log_2(n) \times O(n) = \mathbf{O(n \log n)}$.

4. Multi-Language Implementations: Merge Sort

Python 3 Implementation:

1def merge_sort(arr: list[int]) -> list[int]:
2 if len(arr) <= 1:
3 return arr
4
5 mid = len(arr) // 2
6 # 1. Divide & Conquer
7 left = merge_sort(arr[:mid])
8 right = merge_sort(arr[mid:])
9
10 # 2. Combine (Merge)
11 return merge(left, right)
12 
13def merge(left: list[int], right: list[int]) -> list[int]:
14 merged = []
15 i = j = 0
16
17 # Two-pointer zip merge
18 while i < len(left) and j < len(right):
19 if left[i] <= right[j]: # <= preserves STABILITY!
20 merged.append(left[i])
21 i += 1
22 else:
23 merged.append(right[j])
24 j += 1
25
26 merged.extend(left[i:])
27 merged.extend(right[j:])
28 return merged

Modern JavaScript / TypeScript (In-Place Indices):

1export function mergeSort(arr: number[], low: number = 0, high: number = arr.length - 1): void {
2 if (low >= high) return;
3 
4 const mid = low + Math.floor((high - low) / 2);
5 mergeSort(arr, low, mid);
6 mergeSort(arr, mid + 1, high);
7 merge(arr, low, mid, high);
8}
9 
10function merge(arr: number[], low: number, mid: number, high: number): void {
11 const temp: number[] = [];
12 let i = low;
13 let j = mid + 1;
14 
15 while (i <= mid && j <= high) {
16 if (arr[i] <= arr[j]) {
17 temp.push(arr[i++]);
18 } else {
19 temp.push(arr[j++]);
20 }
21 }
22 
23 while (i <= mid) temp.push(arr[i++]);
24 while (j <= high) temp.push(arr[j++]);
25 
26 for (let k = 0; k < temp.length; k++) {
27 arr[low + k] = temp[k];
28 }
29}

5. Trace Table: Merging Left [27, 38] and Right [9, 43]

Stepleft[i]right[j]Comparison (left[i] <= right[j])Appended to mergedRemaining
1279$27 \le 9$ (False)[9]j advances to 1
22743$27 \le 43$ (True)[9, 27]i advances to 1
33843$38 \le 43$ (True)[9, 27, 38]i exhausted
4-43Remaining right elements[9, 27, 38, 43]Complete!

Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
What is the guaranteed worst-case time complexity of Merge Sort?
Question 2
How much auxiliary memory does standard Merge Sort on an array of size $n$ consume during the merge phase?
Question 3
Why is Merge Sort the preferred sorting algorithm for Singly Linked Lists over Quick Sort?
Question 4
Which comparison operator in the merge function guarantees that Merge Sort remains "Stable"?
Question 5
How many total recursive levels are present in the Merge Sort recursion tree for an input of size $n$?

Next Lesson

Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Elementary Sorting Algorithms: Bubble, Selection & Insertion Sort (O(n²))Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Data Structures & Algorithms with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext