Skip to content

Sorting · routine

Merge two sorted lists

a and b are both sorted. Write merge(a, b) returning one new sorted list with everything from both.

Keep a finger on the front of each list and repeatedly take the smaller one. That is one pass — no sorting needed.

Run adds print(merge([1, 4, 9], [2, 3, 10])) after your code, to try it.

Stuck on the idea rather than the code? The Sorting lesson walks through it.