Two sorted strands braid together into one perfectly ordered sequence.
def merge_sort(arr, l, r):
if l < r:
mid = (l + r) // 2
merge_sort(arr, l, mid)
merge_sort(arr, mid + 1, r)
merge(arr, l, mid, r)
def merge(arr, l, mid, r):
# merge two sorted halves
arr[k] = temp[i] # overwrite
Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.