What is the main idea behind the Merge Sort algorithm?
Merge Sort divides the list into two halves, sorts each half, and then merges the two sorted halves back together.
Click to reveal answer
beginner
What is the time complexity of Merge Sort in the average and worst cases?
The time complexity of Merge Sort is O(n log n) in both average and worst cases.
Click to reveal answer
intermediate
Why does Merge Sort use extra space during sorting?
Merge Sort uses extra space because it creates temporary arrays to hold the divided parts and to merge them back in sorted order.
Click to reveal answer
beginner
Show the step-by-step merge process for merging two sorted arrays: [1, 4, 7] and [2, 3, 6].
Start comparing first elements: 1 < 2, so 1 goes first. Then 4 > 2, so 2 goes next. Then 4 > 3, so 3 goes next. Then 4 < 6, so 4 goes next. Then 7 > 6, so 6 goes next. Finally, 7 goes last. Result: [1, 2, 3, 4, 6, 7].
Click to reveal answer
beginner
What is the base case in the recursive Merge Sort algorithm?
The base case is when the list has one or zero elements, which means it is already sorted and no further division is needed.
Click to reveal answer
What technique does Merge Sort use to sort an array?
ADivide and conquer
BGreedy approach
CDynamic programming
DBrute force
✗ Incorrect
Merge Sort splits the array into halves, sorts them, and merges them back, which is divide and conquer.
What is the space complexity of Merge Sort?
AO(1)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
Merge Sort requires extra space proportional to the size of the array for temporary arrays during merging.
Which of the following is true about Merge Sort's stability?
AMerge Sort is stable
BMerge Sort is unstable
CMerge Sort stability depends on implementation
DMerge Sort is never stable
✗ Incorrect
Merge Sort preserves the order of equal elements, so it is stable.
What happens when the array size reaches 1 in Merge Sort?
AThe algorithm stops completely
BThe array is merged immediately
CThe array is split further
DThe array is considered sorted
✗ Incorrect
An array with one element is already sorted, so recursion stops dividing further.
Which step is NOT part of the Merge Sort algorithm?
AMerging sorted halves
BSorting each half recursively
CSwapping adjacent elements repeatedly
DSplitting the array into halves
✗ Incorrect
Swapping adjacent elements repeatedly is a characteristic of Bubble Sort, not Merge Sort.
Explain how Merge Sort works step-by-step on an unsorted array.
Think about breaking down the problem and combining solutions.
You got /4 concepts.
Describe the advantages and disadvantages of using Merge Sort.
Consider time, space, and stability.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Merge Sort algorithm?
easy
A. Insert elements one by one into a sorted sublist
B. Divide the array into halves, sort each half, then merge them
C. Select a pivot and partition the array around it
D. Sort the array by repeatedly swapping adjacent elements
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
It then merges these sorted halves to produce a fully sorted array.
Final Answer:
Divide the array into halves, sort each half, then merge them -> Option B
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 extra space
2. Which of the following is the correct way to declare the merge function header in C for Merge Sort?
easy
A. void merge(int arr[], int left, int mid, int right);
B. int merge(int arr, int left, int mid, int right);
C. void merge(int arr, int left, int mid, int right);
D. int merge(int arr[], int left, int right);
Solution
Step 1: Check parameter types for merge function
The array should be passed as int arr[] and indices as integers.
Step 2: Confirm return type
Merge function usually returns nothing (void) as it modifies the array in place.
Final Answer:
void merge(int arr[], int left, int mid, int right); -> Option A
Quick Check:
Correct signature = void merge(int arr[], int left, int mid, int right); [OK]
Hint: Array as int arr[], return void for merge [OK]
Common Mistakes:
Passing array as int arr instead of int arr[]
Using int return type instead of void
Missing the mid parameter needed for merging
3. What will be the output of the following code snippet after calling mergeSort(arr, 0, 4); on int arr[] = {5, 3, 8, 4, 2};?
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
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 left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
medium
A. [3, 5, 4, 2, 8]
B. [5, 3, 8, 4, 2]
C. [2, 3, 4, 5, 8]
D. [8, 5, 4, 3, 2]
Solution
Step 1: Understand recursive splitting
The array is split into halves recursively until single elements remain: [5,3,8,4,2] -> [5,3,8] and [4,2], then further splits.
Step 2: Merge sorted halves step-by-step
After sorting and merging, the final array becomes sorted in ascending order: [2, 3, 4, 5, 8].
4. Identify the error in the following merge function snippet used in Merge Sort:
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i <= n1; i++) L[i] = arr[left + i];
for (int j = 0; j <= n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
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 array sizes n1 and n2 are calculated incorrectly
B. The merge function should return an int instead of void
C. The while loops conditions should be i <= n1 and j <= n2
D. The for loops use <= instead of < causing out-of-bounds access
Solution
Step 1: Check for loop boundary errors
The for loops use <= which causes the loop to run one extra time, leading to out-of-bounds array access.
Step 2: Correct loop conditions
Changing <= to < fixes the loops to iterate exactly n1 and n2 times without overflow.
Final Answer:
The for loops use <= instead of < causing out-of-bounds access -> Option D
Quick Check:
Loop boundary off by one = The for loops use <= instead of < causing out-of-bounds access [OK]
Hint: Use <, not <=, for array index loops [OK]
Common Mistakes:
Using <= in loops causing overflow
Confusing return type of merge function
Changing while loop conditions incorrectly
5. Given an array int arr[] = {12, 11, 13, 5, 6, 7};, how many total merge operations will be performed during a full Merge Sort on this array?
hard
A. 5
B. 7
C. 6
D. 8
Solution
Step 1: Understand merge operations count
For an array of size n, Merge Sort performs n-1 merge operations combining subarrays stepwise.
Step 2: Calculate for n=6
Here, n=6, so total merges = 6 - 1 = 5 merges.
Step 3: Count merges carefully
Levels:
- Level 1: 3 merges (pairs of size 1)
- Level 2: 2 merges (subarrays of size 2 and 3)
Total merges = 3 + 2 = 5 merges.
Final Answer:
5 -> Option A
Quick Check:
Count merges per level = 5 [OK]
Hint: Count merges per level, sum all levels [OK]
Common Mistakes:
Assuming merges = n-1 without counting levels
Counting only one level of merges
Confusing merge operations with comparisons
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.