24

Selection Sort

A slow but very easy-to-comprehend sort algorithm is selection sort. Its approach is very simple. Let’s say we have a list of values that we want sorted (Figure 24-1).

Images

FIGURE 24-1

Our starting unsorted values

Selection sort works by finding (aka selecting) the smallest item in your entire list. Once it finds that smallest item, it showers it with love and praise and makes it the first sorted item in a region it carves out called the sorted region. Our selection sort then goes out and finds the next smallest item in the list. Once it finds that item, it places it directly after our earlier smallest item in the sorted region. ...

Get Absolute Beginner's Guide to Algorithms: A Practical Introduction to Data Structures and Algorithms in JavaScript 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.