What is the main idea behind the Merge Sort algorithm?
Merge Sort works by dividing the list into smaller parts, sorting each part, and then joining them back together in order.
Click to reveal answer
beginner
What are the two main steps in the Merge Sort process?
1. Divide the list into halves until each part has one element. 2. Merge the parts back together in sorted order.
Click to reveal answer
intermediate
Why is Merge Sort considered a stable sorting algorithm?
Because it keeps the original order of equal elements when merging them back together.
Click to reveal answer
intermediate
What is the time complexity of Merge Sort in the average and worst cases?
The time complexity is O(n log n) for both average and worst cases, where n is the number of elements.
Click to reveal answer
intermediate
How does Merge Sort use extra space during its execution?
Merge Sort uses extra space to hold temporary lists when merging the divided parts back together.
Click to reveal answer
What is the first step in the Merge Sort algorithm?
ADivide the list into halves
BMerge two sorted lists
CCompare adjacent elements
DSwap elements if out of order
✗ Incorrect
Merge Sort starts by dividing the list into smaller halves before sorting and merging.
Which of the following best describes the merging step in Merge Sort?
ACombining two sorted lists into one sorted list
BSplitting the list into smaller parts
CSwapping elements to sort
DFinding the smallest element
✗ Incorrect
Merging means joining two sorted lists into a single sorted list.
What is the space complexity of Merge Sort?
AO(1) - constant space
BO(n) - linear space
CO(log n) - logarithmic space
DO(n^2) - quadratic space
✗ Incorrect
Merge Sort requires extra space proportional to the size of the list for merging.
Is Merge Sort a stable sorting algorithm?
AOnly for small lists
BNo, it changes the order of equal elements
CYes, it keeps the order of equal elements
DOnly if implemented recursively
✗ Incorrect
Merge Sort preserves the order of equal elements, making it stable.
What is the time complexity of Merge Sort in the worst case?
AO(n^2)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
Merge Sort always runs in O(n log n) time, even in the worst case.
Explain how Merge Sort divides and merges a list step-by-step.
Think about breaking the list down and then building it back up sorted.
You got /4 concepts.
Describe the advantages and disadvantages of using Merge Sort.
Consider speed, memory use, and stability.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Merge Sort algorithm?
easy
A. Swap adjacent elements repeatedly until sorted
B. Divide the list into halves, sort each half, then merge them
C. Pick the smallest element and move it to the front repeatedly
D. Build a sorted list by inserting elements one by one
Solution
Step 1: Understand the divide and conquer approach
Merge Sort splits the list into smaller parts to make sorting easier.
Step 2: Merge sorted halves
After sorting each half, it combines them to form a fully sorted list.
Final Answer:
Divide the list into halves, sort each half, then merge them -> Option B
Quick Check:
Merge Sort = Divide, sort halves, merge [OK]
Hint: Remember: split, sort halves, then merge [OK]
Common Mistakes:
Confusing Merge Sort with Bubble Sort
Thinking it swaps elements directly without splitting
Assuming it sorts in one pass
2. Which of the following is the correct way to split a list arr into two halves in Python for merge sort?
easy
A. left = arr[:len(arr)//2+1]; right = arr[len(arr)//2:]
B. left = arr[1:len(arr)//2]; right = arr[len(arr)//2+1:]
C. left = arr[:len(arr)//2]; right = arr[len(arr)//2:]
D. left = arr[:len(arr)/2]; right = arr[len(arr)/2:]
Solution
Step 1: Use integer division for slicing
Python list slicing requires integer indices; len(arr)//2 gives an integer midpoint.
Step 2: Slice correctly for left and right halves
Left half is from start to midpoint (exclusive), right half from midpoint to end.
Final Answer:
left = arr[:len(arr)//2]; right = arr[len(arr)//2:] -> Option C
Quick Check:
Integer division and slicing = left = arr[:len(arr)//2]; right = arr[len(arr)//2:] [OK]
Hint: Use // for integer division in slicing [OK]
Common Mistakes:
Using single slash / causing float index error
Incorrect slice ranges skipping elements
Adding +1 unnecessarily in slicing
3. What will be the output of the following code snippet?
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
print(merge_sort([4, 1, 3, 2]))
medium
A. [1, 2, 3, 4]
B. [4, 3, 2, 1]
C. [1, 3, 2, 4]
D. [2, 1, 3, 4]
Solution
Step 1: Recursively split and sort the list
The list [4,1,3,2] is split into [4,1] and [3,2], then further until single elements.
Step 2: Merge sorted halves step-by-step
Merge [4] and [1] to get [1,4], merge [3] and [2] to get [2,3], then merge [1,4] and [2,3] to get [1,2,3,4].
Final Answer:
[1, 2, 3, 4] -> Option A
Quick Check:
Sorted list = [1, 2, 3, 4] [OK]
Hint: Trace splits and merges carefully [OK]
Common Mistakes:
Confusing merge order causing wrong output
Not extending remaining elements after main loop
Mixing up indices i and j
4. Identify the error in this merge function used in merge sort:
def merge(left, right):
result = []
i = j = 0
while i < len(left) or j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
return result
medium
A. Using 'or' instead of 'and' in while condition causes index errors
B. Missing initialization of result list
C. Appending elements without comparing
D. Not returning the merged list
Solution
Step 1: Analyze the while loop condition
Using 'or' means loop continues even if one list is exhausted, causing index out of range errors.
Step 2: Correct condition and handle leftovers
The condition should be 'and' to compare elements safely, then extend remaining elements after loop.
Final Answer:
Using 'or' instead of 'and' in while condition causes index errors -> Option A
Quick Check:
While condition must be 'and' to avoid errors [OK]
Hint: Use 'and' in loop to avoid index errors [OK]
Common Mistakes:
Using 'or' causing index out of range
Not extending leftover elements after loop
Forgetting to increment indices properly
5. You want to sort a list of tuples [(3, 'c'), (1, 'a'), (2, 'b')] by the first element using merge sort. Which modification is needed in the merge function's comparison?
hard
A. Compare using if left[i][1] < right[j][1]:
B. Use if left[i] < right[j]: without change
C. Convert tuples to strings before comparing
D. Change comparison to if left[i][0] < right[j][0]:
Solution
Step 1: Understand tuple comparison
Tuples compare element-wise, but to sort by first element explicitly, compare left[i][0] and right[j][0].
Step 2: Modify merge comparison accordingly
Change the if condition in merge to compare the first elements of tuples for correct sorting.
Final Answer:
Change comparison to if left[i][0] < right[j][0]: -> Option D
Quick Check:
Compare tuple first elements for sorting [OK]
Hint: Compare tuple elements explicitly by index [OK]
Common Mistakes:
Comparing whole tuples without index may work but is unclear
Comparing second element instead of first
Converting tuples to strings unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.