Merge Sort is a classic example of a divide-and-conquer algorithm, used for sorting an array or list of elements. It is an efficient, stable, and comparison-based sorting algorithm with a guaranteed time complexity of , making it much more efficient than simpler algorithms like bubble sort or insertion sort, especially for large datasets.
The core idea of merge sort is to divide the array into smaller subarrays, recursively sort these subarrays, and then merge them back together in sorted order. The process follows three main steps:
This recursive breakdown continues until each subarray contains just one element, which is trivially sorted.
Here’s an example implementation of Merge Sort in Python:
def merge_sort(arr):
# Base case: if the array has one or no elements, it's already sorted
if len(arr) <= 1:
return arr
# Divide: Find the middle point and divide the array into two halves
mid = len(arr) // 2
left_half = arr[:mid] # Left half
right_half = arr[mid:] # Right half
# Conquer: Recursively sort both halves
left_sorted = merge_sort(left_half)
right_sorted = merge_sort(right_half)
# Combine: Merge the sorted halves
return merge(left_sorted, right_sorted)
def merge(left, right):
sorted_arr = []
i = j = 0
# Merge two sorted arrays into one
while i < len(left) and j < len(right):
if left[i] < right[j]:
sorted_arr.append(left[i])
i += 1
else:
sorted_arr.append(right[j])
j += 1
# Append any remaining elements from left or right
sorted_arr.extend(left[i:])
sorted_arr.extend(right[j:])
return sorted_arr
merge_sort(arr):
mid), and each half is recursively sorted using the merge_sort function.merge(left, right):
left and right, and merges them into one sorted array.i and j) to iterate through both arrays, comparing the elements and adding the smaller element to the sorted array.The time complexity of Merge Sort is dominated by the two operations: divide and merge.
Therefore, the overall time complexity is:
This is true for both the best, average, and worst cases, making Merge Sort one of the most efficient general-purpose sorting algorithms.
Merge Sort requires extra space for the temporary subarrays during the merge process. Therefore, its space complexity is:
This is because, at each level of recursion, new subarrays are created for merging, and the maximum size of the subarray is (the size of the input array).
Consider an array of numbers:
First division: The array is split into two halves:
Recursively divide each subarray until each subarray has one element:
Merge the subarrays in sorted order:
Combine all the merged subarrays:
Merge Sort is an efficient and stable sorting algorithm that works by recursively dividing the array into two halves, sorting each half, and then merging the sorted halves. Its time complexity is in all cases, and it is especially useful for large datasets or when sorting linked lists. However, its space complexity is , which can be a limitation for very large arrays. Despite this, it is a reliable and often-used sorting algorithm in practice.
Open this section to load past papers