O'Reilly logo

A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Selection Sort in Action

Using the example array [4, 2, 7, 1, 3], our steps would be as follows:

We begin our first passthrough:

We set things up by inspecting the value at index 0. By definition, it’s the lowest value in the array that 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:

images/chapter6/optimizing_code_big_o-centered-no-text_Part7.png

Step #1: We compare the 2 with the lowest value so far (which happens to be 4):

images/chapter6/optimizing_code_big_o-centered-no-text_Part8.png

The 2 is even less than the 4, so it becomes the lowest value so far:

Step #2: We compare the next value—the 7—with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required