Merge Sort as Divide and Conquer in DSA Java - Time & Space Complexity
We want to understand how the time taken by merge sort changes as the list size grows.
How does splitting and merging affect the total work done?
Analyze the time complexity of the following merge sort code.
public void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
This code splits the array into halves, sorts each half, then merges them back.
Look at the parts that repeat as the input grows.
- Primary operation: Splitting the array and merging sorted halves.
- How many times: The array is split into halves repeatedly until single elements remain, then merged back up.
Each split divides the array in half, and merging takes time proportional to the size of the current part.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 40 merges and splits combined |
| 100 | About 700 merges and splits combined |
| 1000 | About 10,000 merges and splits combined |
Pattern observation: The work grows a bit faster than just doubling; it grows by n times log n.
Time Complexity: O(n log n)
This means the time grows a little more than linearly, because the list is split many times and merged.
[X] Wrong: "Merge sort is just like a simple loop, so it should be O(n)."
[OK] Correct: Merge sort splits the list many times and merges each part, so the total work adds up more than a single pass.
Understanding merge sort's time helps you explain how divide and conquer works and why it is efficient for sorting large lists.
"What if we changed merge sort to split the array into three parts instead of two? How would the time complexity change?"
Practice
Merge Sort algorithm in the context of divide and conquer?Solution
Step 1: Understand divide and conquer in Merge Sort
Merge Sort splits the array into two halves recursively until single elements remain.Step 2: Merge sorted halves
It then merges these sorted halves back together to form a sorted array.Final Answer:
Divide the array into halves, sort each half, then merge them -> Option CQuick Check:
Divide and conquer = split, sort, merge [OK]
- Confusing Merge Sort with Bubble Sort
- Thinking Merge Sort uses pivot like Quick Sort
- Ignoring the merge step after dividing
Solution
Step 1: Identify parameters needed for merging
The merge function needs the array and indices: left, mid, right to merge two halves.Step 2: Check method signature correctness
It returns void and takes int[] arr, int left, int mid, int right as parameters.Final Answer:
void merge(int[] arr, int left, int mid, int right) -> Option BQuick Check:
Correct merge signature = void merge(arr, left, mid, right) [OK]
- Using wrong parameter names or missing mid index
- Returning int instead of void
- Confusing start/end with left/right without mid
[4, 1, 3, 9, 7]?
int[] arr = {4, 1, 3, 9, 7};
mergeSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));Solution
Step 1: Apply Merge Sort to the array
Merge Sort divides and merges to sort: [4,1,3,9,7] -> [1,3,4,7,9]Step 2: Verify final sorted array
The sorted array is ascending: [1, 3, 4, 7, 9]Final Answer:
[1, 3, 4, 7, 9] -> Option DQuick Check:
Sorted array = ascending order [OK]
- Confusing merge step output with partial arrays
- Assuming original order remains
- Mixing up ascending and descending order
void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[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];
// merging logic follows...
}Solution
Step 1: Check size calculations for subarrays
n1 should be mid - left + 1 and n2 should be right - mid to cover all elements.Step 2: Identify impact of wrong sizes
Incorrect sizes cause array index errors or missing elements during merge.Final Answer:
Incorrect calculation of n1 size -> Option AQuick Check:
Sizes must include mid element [OK]
- Forgetting +1 in n1 size calculation
- Copying elements starting at wrong index
- Confusing loop bounds with <= instead of <
Solution
Step 1: Understand stability in sorting
Stable sort keeps equal elements in original order.Step 2: Apply stability rule in merge step
When equal, copy element from left subarray first to preserve order.Final Answer:
When elements are equal, copy from the left subarray first -> Option AQuick Check:
Left first on equals = stable merge [OK]
- Copying right element first breaks stability
- Removing duplicates during merge
- Sorting duplicates separately after merge
Ready to prove it?
Pass the test and this pattern moves unknown
