Bubble sort is perfect for an introduction to sorting algorithms. I believe it is the easiest to understand, although not the most efficient, as you will see in a minute.
No surprise here, we loop over every element in the provided array but then again for every element we compare it with the next one, if it is bigger we swap, otherwise we keep on looping until the greatest number ends up at the end. And then we go again and loop until the second greatest ends up at the second place from the end etc...
Let's pause for a second, I mentioned swapping but how does it work? Well there are a few ways of doing this but I prefer this one for its clarity — both your teammates and future self will thank you:
function swap(array, idx, idx2) {
let temp = array[idx];
array[idx] = array[idx2];
array[idx2] = temp;
}
We basically create a temp value storing the first value then we override the first value with the second one and finally we use the stored value to override the second value.
So now that we have this out of the way how does bubbleSort work?
function bubbleSort(arr) {
const sortedArray = [...arr];
for (let i = sortedArray.length; i > 0; i--) {
for (let j = 0; j < i - 1; j++) {
if (sortedArray[j] > sortedArray[j + 1]) {
swap(sortedArray, j, j + 1);
}
}
}
return sortedArray;
}
Let's break this down:
Note: Depending on your needs you can choose to mutate or not the array by copying it first, I did it by default but you should do it based on your use case.
Now what if the array is already sorted before looping over every element? Do we really have to keep looping?
I'd say we don't, so we create a variable which holds the fact that the iteration required swapping, if not we break the loop to prevent unnecessary iterations, here's how it looks!
function bubbleSort(arr) {
let noSwap = true;
const sortedArray = [...arr];
for (let i = sortedArray.length; i > 0; i--) {
noSwap = true;
for (let j = 0; j < i - 1; j++) {
if (sortedArray[j] > sortedArray[j + 1]) {
swap(sortedArray, j, j + 1);
noSwap = false;
}
}
if (noSwap) break;
}
return sortedArray;
}
There you have it an optimized bubble sort, but keep in mind this algorithm is not the most efficient one especially for large datasets!
Bubble sort is a simple and intuitive sorting algorithm, but it comes with significant performance drawbacks. Here are some key points to consider:
Bubble sort is generally not suitable for large datasets due to its inefficiency. However, it can be useful for educational purposes or for sorting small arrays where simplicity and readability are more important than performance.
Bubble sort is a fundamental sorting algorithm that serves as an excellent introduction to the concept of sorting, but its inefficiency for large datasets highlights the need for more advanced algorithms in practical applications.
As you continue your journey in mastering algorithms, remember that choosing the right algorithm depends on the specific requirements and constraints of your problem.
Happy coding ! 👨🏻💻
Honestly i am no one, i've just been coding for 3 years now and i like to document every solutions to every problem i encounter. Extracting as much code snippets and tutorials i can so that if i ever need it again i just have to pop back here and get a quick refresher.
Feel free to me through this learning journey by providing any feedback and if you wanna support me: