Bubble Sort in Action
Let’s walk through a complete example. Assume that we wanted to sort the array [4, 2, 7, 1, 3]. It’s currently out of order, and we want to produce an array containing the same values in the correct, ascending order.
Let’s begin Passthrough #1:
This is our starting array:
Step #1: First, we compare the 4 and the 2. They’re out of order:
Step #2: So we swap them:
Step #3: Next, we compare the 4 and the 7:
They’re in the correct ...
Get A Common-Sense Guide to Data Structures and Algorithms 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.