Why it’s called bubble sort.

ManBearPig
HackerNoon.com
Published in
2 min readOct 6, 2016

--

Bubble sort is a sequential sort algorithm. It sorts by comparing neighbouring elements and swaps them if they are out of order. It just occured to me why it is called bubble sort and how cleverly it was named.

Consider the following bubble sort implementation in Python.

We pass this algorithm an unsorted list. For each index position in the list we compare the element at that position to the element that comes after it. If the element before is greater than the element after then we swap them.

When bubble sort reaches the largest element in the list that element will be pushed forwards again and again until it hits the end. On the next iteration the second largest element will be pushed to the back, and so on.

So why is it called bubble sort? Air bubbles that are more buoyant than surrounding water rise to the top. Similarly, list elements with greater value than their surrounding elements “bubble” to the end of the list.

How interesting!

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--