Selection Sort in Action
Let’s walk through the steps of Selection Sort using the example array, [4, 2, 7, 1, 3].
We begin our first pass-through:
We set things up by inspecting the value at index 0. By definition, it’s the lowest value in the array we’ve encountered so far (as it’s the only value we’ve encountered so far), so we keep track of its index in a variable:
Step 1: We compare the 2 with the lowest value so far (which happens to be 4):
The 2 is even less than the 4, so it becomes the lowest value so far:
Step 2: We compare the next value—the ...
Get A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.