Part 10 — Data Structures and Algorithms in Plain English | Bubble Sort

Loveday Perfection
3 min readMar 2, 2022

--

When you hear the word bubble, what comes to mind? Literal bubbles? As in those containing air and gas? It’s logical to have that be what comes to mind but in the case of computer programming, the word bubble refers not to the noun itself but to the verb. This means bubble, in this case, refers to what happens when a collection of bubbles get created. If you’ve ever seen a collection of bubbles, you’ll notice that upon collision — they will always collide — they slip away across the edges of one another and essentially swap position. On and on and on goes the pattern this all the bubbles fade away.

Now that we’ve gotten the bubble concept, let’s shift our gaze to the bigger picture — Bubble Sort. The word Sort shouldn’t be an unfamiliar concept of programming to you anymore as we covered all of its necessity in the last part of this series. If you missed it then you can catch up on it here. So, if we join our knowledge of sort in programming together with the clarity of bubble we discussed above, we can come to a unified understanding that Bubble Sort has to do with the arrangement (sorting) of things (Arrays, Lists, Strings) in either descending or ascending order by swapping the position of elements to be sorted.

To make it more distinct;

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through a given list or array, compares adjacent elements, and swaps them if they are in the wrong order.

But we don’t just need the definition, do we? We also need to understand how to implement this in a real computer program. Fortunately, implementing bubble sort isn’t any difficult, you simply need to understand the definition we gave above as well as have a basic knowledge of loops and nested loops in any programming language.

  1. Begin an iteration (loop) that spans across the entire length of the array or list.
  2. Then start another iteration inside the above iteration. This new iteration will span across the length of the array minus the current value of the above iteration minus 1. This is because Bubble Sort starts to correctly order the elements from the end of the list or array.
  3. Now, using the second iteration value as the indexer, compare the first and the second element of the list or array.
  4. Depending on your sort order, compare if the elements are greater or less than each other and swap as necessary. For the case of ascending, the first element must be greater than the second element. And in the case of descending, it is the reverse.
  5. The loop will follow this pattern till the last elements of the array or list are compared and swapped as necessary.
  6. This process continues till the first iteration, which spans across the length o the array, is complete.
a python program for Bubble Sort

Notice how the code above follows exactly from the steps we listed above? That’s how easy it is to write a Bubble Sort function. No need to try hard to save the code in your head, just understand the steps.

Now, if you don’t mind, I’d love that you to improve a little on this such that I can specify the sort order I want inside the function — either ascending or descending.

In the next part of this series, we’ll be shifting our focus to Selection Sort and try to see how well it plays over Bubble Sort.

As always, don’t hesitate to give this article a clap if you found it enlightening and stay in tune with me via https://samperfect.me

See you soon 🤗

--

--

Loveday Perfection

herbalist 🌿 — busy doing juju @opensaucerers @paytagafrica @cholatrek_tech @gdg_akure — 📩 wizard@opensaucerer.com