// INPUT_ARRAY
PRESETS:
// HOW_IT_WORKS
BST
Bubble Sort
Repeatedly compares adjacent elements and swaps them if they're in the wrong order. The largest element "bubbles up" to the end each pass.
for i in range(n):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])
✦ Simple to implement
✦ In-place — O(1) memory
✗ Degrades fast with scale
✗ O(n²) worst case
MGS
Merge Sort
Splits the array recursively until single elements remain, then merges sorted halves back together. Divide and conquer at its finest.
def mergeSort(arr): if len(arr) <= 1: return arr mid = len(arr) // 2 L = mergeSort(arr[:mid]) R = mergeSort(arr[mid:]) return merge(L, R)
✦ Guaranteed O(n log n)
✦ Scales to large datasets
✗ Needs O(n) extra memory
✗ More complex to implement
// BIG_O_SPECTRUM
O(1)Constant
O(log n)Logarithmic
O(n)Linear
O(n log n)↑ MERGE
O(n²)↑ BUBBLE
O(2ⁿ)Exponential
► Further right = slower growth. For large n, O(n log n) is orders of magnitude faster than O(n²).