5, 9, 2, 2, 7

This keeps track of the smallest number at the start. So at index 0, it’s 5, right? 9 is bigger, so 5 is still the biggest value. Next one, 2 is smaller, so now the smallest is 2. Great. Repeat, Repeat, 2 is still the smallest at the end of our iteration. So now we switch 5 <-> 2. We know that index 0 is now the smallest value. So we start from index 1 now. Repeat until done.

Did you find this interesting?

Consider subscribing 😊

No AI-generated content or SEO garbage.

Unsubscribe anytime