1.
Introduction
Merge Sort is a divide-and-conquer sorting algorithm. It splits the list into smaller parts, sorts
them, and then merges them back together. It is efficient for large datasets.
2. How It Works
1. Divide the array into two halves.
2. Recursively sort each half.
3. Merge the sorted halves into a single sorted array.
3. Pseudocode
sql
CopyEdit
procedure mergeSort(A):
if length(A) > 1:
mid = length(A) // 2
left = A[0...mid-1]
right = A[mid...end]
mergeSort(left)
mergeSort(right)
merge(left, right, A)
4. Example
Input: [38, 27, 43, 3, 9, 82, 10]
Output: [3, 9, 10, 27, 38, 43, 82]
5. Complexity Analysis
Time Complexity: O(n log n) in all cases
Space Complexity: O(n) extra space
Stability: Stable