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) for both average and worst cases.
Click to reveal answer
intermediate
Why does Merge Sort require extra space?
Merge Sort needs extra space to hold temporary arrays during the merging process because it combines two sorted lists into one.
Click to reveal answer
intermediate
Explain the merge step in Merge Sort.
The merge step takes two sorted halves and compares their elements one by one, placing the smaller element into a new array until all elements are merged in sorted order.
Click to reveal answer
intermediate
Is Merge Sort a stable sorting algorithm? Why or why not?
Yes, Merge Sort is stable because it preserves the relative order of equal elements during the merge step.
Click to reveal answer
What technique does Merge Sort use to sort the array?
ADivide and conquer
BGreedy
CDynamic programming
DBrute force
✗ Incorrect
Merge Sort splits the array into smaller parts, sorts them, and merges them back, which is divide and conquer.
What is the space complexity of Merge Sort?
AO(1)
BO(n log n)
CO(log n)
DO(n)
✗ Incorrect
Merge Sort requires O(n) extra space for temporary arrays during merging.
During the merge step, what happens when elements from both halves are equal?
AThe element from the right half is chosen first
BThe element from the left half is chosen first
CBoth elements are skipped
DThe algorithm stops
✗ Incorrect
To maintain stability, the element from the left half is chosen first when elements are equal.
Which of the following is true about Merge Sort's performance?
AIt always divides the array into halves
BIt has a worst-case time complexity of O(n^2)
CIt is faster than Quick Sort in all cases
DIt sorts in place without extra memory
✗ Incorrect
Merge Sort always divides the array into two halves recursively.
What is the base case for the recursive Merge Sort function?
AWhen the array has more than two elements
BWhen the array is fully sorted
CWhen the array has one or zero elements
DWhen the array is empty
✗ Incorrect
The base case is when the array has one or zero elements, which are already sorted.
Describe step-by-step how Merge Sort sorts an array of numbers.
Think about splitting, sorting smaller parts, and merging.
You got /3 concepts.
Explain why Merge Sort is considered a stable sorting algorithm.
Focus on how equal elements are handled during merging.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind the Merge Sort Algorithm?
easy
A. Select the smallest element and place it at the beginning
B. Divide the array into halves, sort each half, then merge them
C. Sort the array by repeatedly swapping adjacent elements
D. Build a heap and extract elements in sorted order
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, Sort, Merge [OK]
Hint: Remember: split, sort, then merge [OK]
Common Mistakes:
Confusing merge sort with bubble sort
Thinking merge sort swaps adjacent elements
Mixing merge sort with selection sort
2. Which of the following is the correct way to declare the merge function header in C++ for merge sort?
easy
A. int mergeSort(int arr[], int left, int right);
B. int merge(int arr[], int left, int mid, int right);
C. void mergeSort(int arr[], int left, int right);
D. void merge(int arr[], int left, int mid, int right);
Solution
Step 1: Identify the merge function role
The merge function combines two sorted parts; it does not return a value, so it should be void.
Step 2: Check parameter list
It takes the array and three indices: left, mid, and right to merge subarrays.
Final Answer:
void merge(int arr[], int left, int mid, int right); -> Option D
Quick Check:
Merge function = void with arr and indices [OK]
Hint: Merge combines parts, returns nothing [OK]
Common Mistakes:
Using int return type for merge
Confusing merge with mergeSort function
Wrong parameter order or missing parameters
3. What will be the output of the following code snippet after applying merge sort on the array?
int arr[] = {5, 2, 9, 1};
mergeSort(arr, 0, 3);
for(int i = 0; i < 4; i++) std::cout << arr[i] << " ";
medium
A. 5 2 9 1
B. 9 5 2 1
C. 1 2 5 9
D. 2 1 5 9
Solution
Step 1: Apply merge sort on the array
The array {5, 2, 9, 1} is split and sorted recursively to {1, 2, 5, 9}.
Step 2: Print the sorted array
The for loop prints elements in sorted order: 1 2 5 9.