Splits the list into smaller parts, sorts both halves, then merges them back in order.
Numbered boxes show both the value and its current position number. Movement is animated from one slot to another. For divide-and-conquer algorithms, the page also shows how the list splits into parts and then comes back together.
This area becomes especially useful for Merge Sort, Quick Sort, and TimSort-style run/merge explanation. It visually shows parts, subparts, pivot-based partitions, or natural runs depending on the algorithm.
Tip: try arrays with duplicates and partially sorted values. Watching the movements helps understand why some algorithms preserve order and some do not.
def merge_sort(arr):
if len(arr) <= 1:
return arr[:]
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
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 resultThe editor URL below has been updated to remove lesson-viewer. The code is packed into the hash so the editor can load a ready example directly.