~/ learn/ comp-372/ cards/ Merge sort & divide-and-conquer
1 of 4

Type the MERGE inner loop (pick the smaller front element)

Type the MERGE inner loop (pick the smaller front element)

Answer

while i < len(left) and j < len(right): if left[i] <= right[j]: out.append(left[i]); i += 1 else: out.append(right[j]); j += 1

Each comparison emits exactly one element, so the loop runs ≤ n times; after it, one side is exhausted and the remainder of the other is appended directly. Using `<=` (not `<`) keeps equal keys in left-then-right order — that is what makes merge sort stable.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/6870d3ef-781d-4dae-849d-57704711e972/flashcard utf-8 LF