Data Structure & Algorithm Prof.
Muzamil Umar (KFGC)
Merge Sort
1. Introduction
Merge Sort is a Divide and Conquer sorting algorithm.
It divides a large problem into smaller sub-problems, solves them, and then combines
the results.
The array is repeatedly divided into two halves until each sub-array contains only one
element.
After dividing, the elements are merged in sorted order.
Merge Sort is efficient and suitable for large datasets.
It always takes the same time complexity in best, average, and worst cases.
This algorithm is widely used in computer science for stable sorting.
It is especially useful when dealing with large lists or linked lists.
However, it requires additional memory for merging.
Merge Sort guarantees O(n log n) performance.
2. Working Principle
Merge Sort follows the Divide and Conquer technique.
1. Divide → Split the array into two halves.
2. Conquer → Recursively sort both halves.
3. Combine → Merge the sorted halves into one sorted array.
3. Step-by-Step Example
Example Array:
[38, 27, 43, 3, 9, 82, 10]
Step 1: Divide
[38, 27, 43, 3] [9, 82, 10]
Divide again:
[38, 27] [43, 3] [9, 82] [10]
Divide until single elements:
[38] [27] [43] [3] [9] [82] [10]
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
Step 2: Merge and Sort
[38] [27] → [27, 38]
[43] [3] → [3, 43]
[9] [82] → [9, 82]
Next merge:
[27, 38] [3, 43] → [3, 27, 38, 43]
[9, 82] [10] → [9, 10, 82]
Final merge:
[3, 27, 38, 43] [9, 10, 82]
→ [3, 9, 10, 27, 38, 43, 82]
Sorted Array.
4. Merge Sort Algorithm
Merge Sort Function
MergeSort(arr, left, right)
1. if left < right
2. mid = (left + right) / 2
3. MergeSort(arr, left, mid)
4. MergeSort(arr, mid + 1, right)
5. Merge(arr, left, mid, right)
Merge Function
Merge(arr, left, mid, right)
1. Create two temporary arrays
2. Compare elements of both arrays
3. Place the smaller element into the original array
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
4. Copy remaining elements if any
5. C++ Implementation
#include <iostream> // Library for input and output
using namespace std; // Use standard namespace
// Function to merge two sorted subarrays
void merge(int arr[], int left, int mid, int right)
{
int n1 = mid - left + 1; // Calculate size of left subarray
int n2 = right - mid; // Calculate size of right subarray
int L[n1], R[n2]; // Temporary arrays to store subarrays
for(int i = 0; i < n1; i++) // Copy elements of left part into L[]
L[i] = arr[left + i]; // Store left subarray elements
for(int j = 0; j < n2; j++) // Copy elements of right part into R[]
R[j] = arr[mid + 1 + j]; // Store right subarray elements
int i = 0, j = 0, k = left; // i for L[], j for R[], k for main array
while(i < n1 && j < n2) // Compare elements of both subarrays
{
if(L[i] <= R[j]) // If element of L is smaller
{
arr[k] = L[i]; // Place it in original array
i++; // Move to next element in L
}
else // Otherwise
{
arr[k] = R[j]; // Place element from R in array
j++; // Move to next element in R
}
k++; // Move to next position in main array
}
while(i < n1) // Copy remaining elements of L[] if any
{
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
arr[k] = L[i]; // Copy element into main array
i++; // Move forward in L
k++; // Move forward in main array
}
while(j < n2) // Copy remaining elements of R[] if any
{
arr[k] = R[j]; // Copy element into main array
j++; // Move forward in R
k++; // Move forward in main array
}
}
// Recursive function to perform Merge Sort
void mergeSort(int arr[], int left, int right)
{
if(left < right) // Check if more than one element
{
int mid = (left + right) / 2; // Find middle index
mergeSort(arr, left, mid); // Recursively sort left half
mergeSort(arr, mid + 1, right); // Recursively sort right half
merge(arr, left, mid, right); // Merge the two sorted halves
}
}
int main()
{
int arr[] = {38, 27, 43, 3, 9, 82, 10}; // Declare and initialize array
int n = 7; // Size of array
mergeSort(arr, 0, n - 1); // Call Merge Sort function
cout << "Sorted array: "; // Print message
for(int i = 0; i < n; i++) // Loop to print sorted array
cout << arr[i] << " "; // Display each element
return 0; // End of program
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
6. Time Complexity
Case Complexity
Best Case O(n log n)
Average Case O(n log n)
Worst Case O(n log n)
7. Space Complexity
Merge Sort requires extra memory.
Space Complexity = O(n)
8. Advantages
✔ Efficient for large datasets
✔ Stable sorting algorithm
✔ Predictable time complexity
✔ Works well with linked lists
9. Disadvantages
Requires additional memory
Slower than Quick Sort in some practical cases
Not suitable for very small arrays
10. Important Points
• Merge Sort uses Divide and Conquer strategy.
• It divides the array until single elements remain.
• Sorted subarrays are merged to form a sorted array.
• Time complexity is always O(n log n).
• It is a stable sorting algorithm.
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
MCQs
1. Merge Sort is based on which technique?
A) Greedy Method
B) Divide and Conquer
C) Dynamic Programming
D) Backtracking
Answer: B
2. Merge Sort divides the array until each subarray contains:
A) Two elements
B) Three elements
C) One element
D) Four elements
Answer: C
3. The main operation in Merge Sort is:
A) Swapping
B) Searching
C) Merging
D) Hashing
Answer: C
4. Best case time complexity of Merge Sort is:
A) O(n)
B) O(n log n)
C) O(log n)
D) O(n²)
Answer: B
5. Worst case time complexity of Merge Sort is:
A) O(n log n)
B) O(n²)
C) O(n)
D) O(log n)
Answer: A
6. Average case time complexity of Merge Sort is:
A) O(n)
B) O(n²)
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
C) O(n log n)
D) O(log n)
Answer: C
7. Space complexity of Merge Sort is:
A) O(1)
B) O(log n)
C) O(n)
D) O(n²)
Answer: C
8. Merge Sort is:
A) In-place sorting algorithm
B) Out-of-place sorting algorithm
C) Non-comparison sorting
D) Hashing algorithm
Answer: B
9. Merge Sort is a:
A) Stable sorting algorithm
B) Unstable sorting algorithm
C) Random algorithm
D) Greedy algorithm
Answer: A
10. Merge Sort works efficiently for:
A) Small datasets only
B) Large datasets
C) Only sorted arrays
D) Only integers
Answer: B
11. Merge Sort is commonly used with:
A) Graphs
B) Linked Lists
C) Stacks
D) Queues
Answer: B
12. In Merge Sort, the middle index is calculated as:
A) left + right
B) (left + right) / 2
C) left × right
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
D) left − right
Answer: B
13. Merge Sort algorithm uses recursion because:
A) It repeatedly divides the array
B) It swaps elements
C) It searches elements
D) It deletes elements
Answer: A
14. After dividing the array completely in Merge Sort, the next step is:
A) Swapping
B) Merging
C) Deleting
D) Searching
Answer: B
15. Merge Sort requires extra memory for:
A) Recursion only
B) Temporary arrays
C) Stack operations
D) Queue operations
Answer: B
16. Merge Sort compares elements from:
A) Two sorted subarrays
B) Two random arrays
C) Entire array at once
D) Only one array
Answer: A
17. Merge Sort performs merging in:
A) Random order
B) Descending order only
C) Sorted order
D) Reverse order
Answer: C
18. Merge Sort is slower than Quick Sort in:
A) Worst case
B) Best case
C) Practical implementations sometimes
D) Always
Answer: C
Data Structure & Algorithm Prof. Muzamil Umar (KFGC)
19. The number of levels in Merge Sort recursion tree is approximately:
A) log n
B) n
C) n²
D) √n
Answer: A
20. Merge Sort is preferred when:
A) Memory is limited
B) Stable sorting is required
C) Data is very small
D) No recursion is allowed
Answer: B