22

Bubblesort

When it comes to sorting stuff, one of the worst algorithms you can use is known affectionately as bubblesort. It is sooo terrible that people have stopped naming kids after it. Despite its terribleness, it is important for you to learn how bubblesort works. The main reason is to understand what it does and avoid doing anything in your code that even remotely resembles it. The other reason is purely for trivial purposes. You’ll never know when this is belted out as a Final Jeopardy answer one day.

Onward!

How Bubblesort Works

Let’s say you have a collection of numbers that are currently unsorted (Figure 22-1).

Images

FIGURE 22-1

Our ...

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.