DEV Community 👩‍💻👨‍💻

Amber-Yerkey
Amber-Yerkey

Posted on

What is Bubble Sorting?

This is a comparison sorting algorithm that works by swapping the adjacent elements until they are in the correct order. This algorithm does not have a good performance and is not scalable. It is not often used but is a good introduction to learning about algorithms.

For example, in array [1,3,2,4] the algorithm would compare the first two numbers and since 1 < 3, nothing changes. Our array stays the same [1,3,2,4]. However, it then goes on to compare the next two numbers and since 3 > 2, it swaps them. Our array now looks like this [1,2,3,4]. It proceeds to compare our last two numbers but since 3 & 4 is in the correct order, nothing changes. The algorithm does another full pass of these numbers to make sure there isn't any more sorting to be done.

This is just for a simple 4 numbers. If there are more swaps to be made, it will keep going back over the list until there is no longer a swap. This can easily get out of hand with a large amount of elements in a list. And the more elements, the worse the performance.

Trying to make this work in a programming language on a small scale would be good practice for any language.

For further research you could include other algorithms, as well as tradeoffs for each (such as performance). Learning about "Big O Notation" will also help you identify best algorithms by performance.

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠