Exercises
-
20.5 (Bubble Sort) Implement the bubble sort algorithm—another simple yet inefficient sorting technique. It’s called bubble sort or sinking sort because smaller values gradually “bubble” their way to the top of the
array(i.e., toward the first element) like air bubbles rising in water, while the larger values sink to the bottom (end) of thearray. The technique uses nested loops to make several passes through thearray. Each pass compares successive pairs of elements. If a pair is in increasing order (or the values are equal), the bubble sort leaves the values as they are. If a pair is in decreasing order, the bubble sort swaps their values in thearray.The first pass compares the first two element values of the
arrayand swaps ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access