Merge Sort
Merge Sort
Merge Sort is a stable, divide-and-conquer sorting algorithm. It recursively splits the input array into two halves, sorts each half individually, and merges the sorted sub-arrays. It ensures consistent O(N log N) speeds, though it requires auxiliary memory to merge elements.
Complexity Profile
| Case | Complexity |
|---|---|
| Best Case | O(N log N) |
| Average Case | O(N log N) |
| Worst Case | O(N log N) |
| Space Complexity | O(N) |
Code Implementation
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return resultReal-World Applications
- Sorting linked lists without access penalty.
- External sorting where dataset exceeds system memory capabilities.
- E-commerce sorting systems requiring stable matching order.
Architectural Analysis
[!tip] Deep Dive Best sorting algorithm when stability (preserving order of duplicate values) is required and worst-case O(N log N) performance is a strict requirement. Unlike Quick Sort, which can degrade to O(N^2) for pre-sorted or adversarial inputs, Merge Sort guarantees consistent O(N log N) speed at the expense of O(N) temporary space.
Linear Search
Linear search is the simplest search algorithm. It scans elements of a sequence sequentially, one by one, checking whether the target element matches the current element. This is useful for unsorted arrays or when data is simple and unsorted, though highly inefficient for larger arrays.
Quick Sort
Quick Sort is a divide-and-conquer sorting algorithm. It selects a 'pivot' element and partitions the array such that elements smaller than the pivot go to the left, and larger ones go to the right, before recursively sorting the sub-arrays. Highly efficient in-place sorting utility.