📖 Scenario: You have a list of numbers that you want to sort from smallest to largest. Sorting helps you find things faster, like organizing books by their titles on a shelf.
🎯 Goal: You will build a program that sorts a list of numbers using the merge sort method. Merge sort splits the list into smaller parts, sorts those parts, and then joins them back together in order.
📋 What You'll Learn
Create an array with exact numbers
Create a variable for the array size
Write a function to merge two sorted parts
Write a function to split and sort the array using merge sort
Print the sorted array
💡 Why This Matters
🌍 Real World
Sorting is used everywhere, like organizing files, searching data quickly, and arranging items in online stores.
💼 Career
Understanding merge sort helps in software development, data analysis, and any job that requires efficient data handling.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called arr with these exact values: 38, 27, 43, 3, 9, 82, 10. Also create an integer variable called n and set it to the size of arr.
DSA C
Hint
Use int arr[] = {38, 27, 43, 3, 9, 82, 10}; to create the array and int n = sizeof(arr) / sizeof(arr[0]); to get its size.
2
Write the merge function
Write a function called merge that takes the array arr, and three integers l, m, and r. This function will merge two sorted parts of the array: from l to m and from m+1 to r. Use temporary arrays to help merge in sorted order.
DSA C
Hint
Use two temporary arrays to hold the left and right parts. Then compare elements and copy the smaller one back to arr.
3
Write the mergeSort function
Write a function called mergeSort that takes the array arr, and two integers l and r. This function will split the array into halves, call itself recursively to sort each half, and then call merge to join the halves in order.
DSA C
Hint
Split the array by finding the middle index. Call mergeSort on left and right halves, then merge them.
4
Print the sorted array
Call mergeSort on the array arr with l = 0 and r = n - 1. Then use a for loop with variable i from 0 to n - 1 to print each element of arr separated by a space.
DSA C
Hint
Call mergeSort(arr, 0, n - 1); to sort. Then print each element with a space using a for loop.
Practice
(1/5)
1. What is the main idea behind the Merge Sort algorithm?
easy
A. Divide the array into halves, sort each half, then merge them
B. Sort the array by repeatedly swapping adjacent elements
C. Select a pivot and partition the array around it
D. Insert elements one by one into a sorted sublist
Solution
Step 1: Understand the divide and conquer approach
Merge Sort splits the array into two halves recursively until single elements remain.
Step 2: Merge sorted halves
After splitting, it merges the sorted halves back to form a sorted array.
Final Answer:
Divide the array into halves, sort each half, then merge them -> Option A
Quick Check:
Merge Sort = Divide and Conquer [OK]
Hint: Remember: split, sort halves, then merge [OK]
Common Mistakes:
Confusing Merge Sort with Bubble Sort
Thinking Merge Sort uses a pivot like Quick Sort
Believing Merge Sort sorts in place without merging
2. Which of the following is the correct base case condition in a recursive Merge Sort function in C?
easy
A. if (start >= end) return;
B. if (start > end) return;
C. if (start == end) return;
D. if (start != end) return;
Solution
Step 1: Identify when recursion should stop
Merge Sort stops splitting when the subarray has one or zero elements, i.e., start >= end.
Step 2: Choose the correct condition
The base case is when start >= end, so return without further splitting.
Final Answer:
if (start >= end) return; -> Option A
Quick Check:
Base case = start >= end [OK]
Hint: Stop recursion when start >= end [OK]
Common Mistakes:
Using == instead of >= risks issues if start > end
Using > misses the single element case
Using != returns too early, breaking recursion
3. What will be the output of the following C code snippet after calling mergeSort(arr, 0, 4); on int arr[] = {5, 3, 8, 4, 2};?
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {5, 3, 8, 4, 2};
mergeSort(arr, 0, 4);
for (int i = 0; i < 5; i++) printf("%d ", arr[i]);
return 0;
}
medium
A. 5 3 8 4 2
B. 8 5 4 3 2
C. 2 3 4 5 8
D. 2 4 3 5 8
Solution
Step 1: Understand mergeSort splits and merges
The array is recursively split into halves and merged in sorted order.
Step 2: Trace the final sorted array
After full recursion and merging, the array becomes sorted: 2 3 4 5 8.
Final Answer:
2 3 4 5 8 -> Option C
Quick Check:
Sorted array output = 2 3 4 5 8 [OK]
Hint: Merge Sort always outputs sorted array [OK]
Common Mistakes:
Thinking mergeSort does not change original array
Confusing mergeSort with reverse sorting
Ignoring the merge step results in unsorted output
4. Identify the error in the following merge function snippet used in Merge Sort:
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i <= n1; i++) L[i] = arr[l + i];
for (int j = 0; j <= n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
medium
A. The merge function should not use temporary arrays
B. The loops copying to L and R arrays use <= instead of < causing out-of-bounds access
C. The size of arrays L and R is calculated incorrectly
D. The merge condition should be L[i] < R[j] instead of <= to avoid duplicates
Solution
Step 1: Check array copying loops
The loops use i <= n1 and j <= n2 which causes index to go out of bounds since arrays are 0-based.
Step 2: Correct loop condition
Loops should use i < n1 and j < n2 to avoid accessing invalid memory.
Final Answer:
The loops copying to L and R arrays use <= instead of < causing out-of-bounds access -> Option B
Quick Check:
Loop bounds must be less than array size [OK]
Hint: Use < not <= in loops for array indexing [OK]
Common Mistakes:
Using <= causes buffer overflow errors
Confusing array size with last index
Ignoring array indexing starting at 0
5. You want to sort a linked list using Merge Sort. Which of the following is the best approach to split the list into two halves efficiently?
hard
A. Convert the list to an array, sort it, then rebuild the list
B. Count the total nodes first, then traverse again to the middle node
C. Split the list by breaking after the first node repeatedly
D. Use two pointers: move one pointer twice as fast as the other to find the middle
Solution
Step 1: Understand splitting linked list for Merge Sort
Using two pointers (slow and fast), where fast moves twice as fast, helps find the middle in one pass.
Step 2: Compare other methods
Counting nodes requires two passes; breaking after first node is incorrect; converting to array is inefficient.
Final Answer:
Use two pointers: move one pointer twice as fast as the other to find the middle -> Option D
Quick Check:
Two-pointer technique = efficient split [OK]
Hint: Use slow-fast pointer to find middle in one pass [OK]
Common Mistakes:
Counting nodes wastes time with two traversals
Breaking after first node does not split evenly
Converting to array loses linked list benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.