Implement Bubble Sort in JavaScript
Michael Mitrakos
4 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
Implement Bubble Sort in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
This article is part of a series covering sort algorithms in JavaScript. You can find the rest of the series here. If you’re new to sorting algorithms, or algorithms in general, read this first to get a solid foundation for moving forward.
While we do want to learn the most efficient algorithms, studying the most inefficient algorithms can also give us context about what exactly a good algorithm is, what it looks like, and why it’s considered ‘good’. Bubble sort is by far one of the worst sorting algorithms and should (most likely) never be used in production code, so keep that in mind as we move forward :)
This is often the easiest to conceptualize and a natural way for the brain to think about sorting. Bubble sort gets its name from what a visualization might look like as each number is bubbled up to the front of the array space by space. It’s a quadratic sorting algorithm because it uses an iterative approach by continuously looping and swapping elements until they are in the correct order.
Elements are only swapped with other elements that are one index away. This can lead to a large number of swaps because it’s so inefficient. Algorithms like insertion sort and selection sort perform drastically better.
Complexity
The worst case scenario of this algorithm is quadratic — O(n²) — because it’s possible that the data will be the exact opposite of ordered. Best case scenario is linear — O(n) — because even if it’s entirely ordered, we have to check through each set of numbers.
The space complexity of Bubble Sort is O(1).
When it’s fast
There is one case where bubble sort is fairly efficient. This is when the list is sorted or almost entirely sorted, therefore it would only require swapping a few elements that are only one index away. However, this is only true if you use an optimized algorithmic approach to bubble sort which tracks if no swaps take place and therefore exits the sort early. I’ll explore the code for this algorithm below.
When Bubble Sort in JavaScript is Useful
Bubble Sort is a straightforward yet less efficient sorting algorithm in JavaScript, suitable for small data sets or educational purposes. This algorithm repeatedly compares adjacent elements and swaps them if they are in the wrong order, gradually moving larger elements towards the end of the array. With its time complexity of O(n²), Bubble Sort is less efficient compared to other sorting algorithms, making it less suitable for large or nearly sorted arrays. However, Bubble Sort has the advantage of simplicity and ease of implementation. JavaScript developers can utilize Bubble Sort to gain a better understanding of sorting algorithms or handle sorting tasks on small data sets where efficiency is not a primary concern.
Pseudocode
If current value is greater than next value
Set swap variable to true
return sliced array variable
Code
Below is the optimized version of bubble sort that will exit the sort function if no swaps take place. The key here is to use a while loop where it can track if it doesn’t attempt to sort elements after the element that was swapped last in the most previous iteration.
Congratulations on completing (possibly) your first sorting algorithm! Next up is insertion sort :)
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
I founded Higglo Digital and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a beautifully designed website, check us out.
I also created Wanderlust Extension to discover the most beautiful places across the world with highly curated content. Check it out!