Merge Sort – Report
1. Introduction
Merge Sort is an efficient, general-purpose, comparison-based divide-and-conquer sorting
algorithm. It was invented by John von Neumann in 1945. Merge Sort divides the input array into
two halves, sorts them recursively, and then merges the sorted halves.
2. How It Works
Merge Sort follows the divide-and-conquer strategy:
1. Divide: Split the array into two halves.
2. Conquer: Recursively sort both halves.
3. Combine: Merge the sorted halves into a single sorted array.
3. Algorithm (Pseudocode)
plaintext
CopyEdit
MergeSort(arr[], l, r)
if l < r:
m = (l + r) / 2
MergeSort(arr, l, m)
MergeSort(arr, m+1, r)
Merge(arr, l, m, r)
Merge(arr[], l, m, r)
Create temporary arrays L[] and R[]
Copy data to L[] and R[]
Merge L[] and R[] into arr[]
4. Time and Space Complexity
Case Time Complexity
Best Case O(n log n)
Average Case O(n log n)
Case Time Complexity
Worst Case O(n log n)
• Space Complexity: O(n) – due to the use of temporary arrays during the merge step.
5. Example
For the array: [38, 27, 43, 3, 9, 82, 10]
1. Divide: [38, 27, 43] and [3, 9, 82, 10]
2. Further divide until subarrays of size 1
3. Merge step-by-step:
o [27, 38, 43], [3, 9, 10, 82]
o Final merge: [3, 9, 10, 27, 38, 43, 82]