Swap until settled
transcript
Find the smallest missing positive id. You may only use the list itself. Send each value to its own slot.
A value in range is swapped to its slot. A value out of range is skipped. A value already home is skipped too.
With one swap, the one is left misplaced. The scan then reports one as missing. Loop the swap. The answer becomes two.
A swap fills one slot correctly. It also brings another value to position i. That value may also be misplaced.
After placement, slot i should hold i plus one. The first slot that fails names the answer. If none fail, the answer is n plus one.
No extra memory is used. The list is the table. Each swap sends one value to its final slot. So the total number of swaps is at most n.
Use while, not if. Skip values out of range. Scan for the first wrong slot. Full breakdown in the lecture.