Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Merge Sort quickly and effectively.
316+ students studying
Merge Sort is a highly efficient, comparison-based sorting algorithm that uses the divide-and-conquer approach. It divides the input array into smaller sub-arrays, sorts them recursively, and then merges the sorted sub-arrays back together. Merge Sort is particularly effective for large data sets and ensures a consistently good performance.
Given an unsorted list:
38, 27, 43, 3, 9, 82, 10
Step 1: Split the list recursively until each sublist has one element.
38, 27, 43, 3, 9, 82, 10 → 38, 27, 43, 3, 9, 82, 10
→ 38, 27, 43, 3, 9, 82, 10
→ 38, 27, 43, 3, 9, 82, 10
Step 2: Merge pairs of sublists in sorted order.
27, 38, 3, 43, 9, 82, 10
Step 3: Merge again.
3, 27, 38, 43, 9, 10, 82
Step 4: Final merge to get the sorted list.
3, 9, 10, 27, 38, 43, 82
METHOD MergeSort(array)
IF array.length > 1
mid ← array.length DIV 2
left ← array[0 TO mid - 1]
right ← array[mid TO array.length - 1]
CALL MergeSort(left)
CALL MergeSort(right)
CALL Merge(left, right, array)
METHOD Merge(left, right, array)
i ← 0 // Index for left subarray
j ← 0 // Index for right subarray
k ← 0 // Index for merged array
WHILE i < left.length AND j < right.length
IF left[i] ≤ right[j]
array[k] ← left[i]
i ← i + 1
ELSE
array[k] ← right[j]
j ← j + 1
k ← k + 1
WHILE i < left.length
array[k] ← left[i]
i ← i + 1
k ← k + 1
WHILE j < right.length
array[k] ← right[j]
j ← j + 1
k ← k + 1
def merge_sort(array):
if len(array) > 1:
mid = len(array) // 2
left_half = array[:mid]
right_half = array[mid:]
# Recursively sort both halves
merge_sort(left_half)
merge_sort(right_half)
# Merge sorted halves
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] <= right_half[j]:
array[k] = left_half[i]
i += 1
else:
array[k] = right_half[j]
j += 1
k += 1
# Copy any remaining elements in left_half
while i < len(left_half):
array[k] = left_half[i]
i += 1
k += 1
# Copy any remaining elements in right_half
while j < len(right_half):
array[k] = right_half[j]
j += 1
k += 1
# Example usage:
data = [38, 27, 43, 3, 9, 82, 10]
merge_sort(data)
print(data) # Output: [3, 9, 10, 27, 38, 43, 82]
Example Given the list 8, 3, 7, 4, 6, 2, 5, 1
i
, j
, k
) correctly while merging can cause out-of-bound errors or incorrect merges.Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
120 flashcards
Flashcards on Merge Sort
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards12 quizzes
Quizzes on Merge Sort
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Merge Sort
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Merge Sort
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Merge Sort
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Merge Sort to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered