Algorithms — Bubble Sort

Elijah Wines
3 min readJan 2, 2023

Today we will be discussing algorithms, and more specifically, the Bubble Sort algorithm. When I first began my coding journey I thought I would be able to get by without diving deep into topics like design patterns and algorithms. I soon realized that not only are these topics essential, they are also very beneficial when it comes to developing a deeper understanding of writing code. So why learn algorithms? In programming, algorithms mimic real life problems and provide real life solutions to those problems. Not only do algorithms help deepen your understanding of writing code, they also help you solve common problems you will encounter while creating applications. They are great mental exercises and they force you to plan ahead before diving straight into your code. Let’s take a look at a common sorting algorithm, the Bubble Sort algorithm.

What is the Bubble Sort Algorithm?

It is a sorting algorithm that repeatedly steps through a list, compares adjacent elements and swaps them if they are not in the correct order. This step is repeated until the list is completely sorted. The algorithm gets its name from the way the smaller elements “bubble” to the top of the list.

Step-By-Step:

  1. Compare the first two elements in the list. If the first is greater than the second, swap them.
  2. Compare the second and third elements in the list. If the second is greater than the third, swap them.
  3. Continue this process until you reach the end of the list.
  4. If any swaps were made during the current pass, repeat the process again from the beginning of the list.

When should I use the Bubble Sort Algorithm?

The Bubble Sort algorithm is great for sorting lists of a smaller size. This is because this algorithm has a time complexity of O(n²), quadratic time complexity, which means this algorithm could cause a significant amount of loops before the task is complete.

To put it simply, this algorithm is not efficient for sorting large lists because if we have a list with a size of 10, we may have to iterate over this list 10² times. You can imagine that with larger lists, this algorithm may take a while. While the time complexity can be a cause for concern, this algorithm is very easy to implement, and can be useful in certain situations.

Example:

Let’s take a look at a Bubble Sort algorithm in action.

function bubbleSort(arr) {
let swapped;
do {
swapped = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
return arr;
}

Our function, bubbleSort, takes an array and sorts it using the Bubble Sort algorithm. The function continues to loop through the array until no swaps are made, at which point the array is sorted.

Let’s make sure it works!

var nums = [2,7,12,45,43,67,98,43,5,23,4,9,34,21,34,5];

console.log(bubbleSort(nums));
// returns [2, 4, 5, 5, 7, 9, 12, 21, 23, 34, 34, 43, 43, 45, 67, 98]

Excellent! Our Bubble Sorting algorithm is working exactly how we wanted.

Conclusion

In this article we learned about the Bubble Sorting algorithm. We discussed how it works, when its useful and not useful, and went over an example of the algorithm using JavaScript. I will continue to learn more algorithms and implement them into my projects, and I hope you do the same! Keep learning, and happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Elijah Wines
Elijah Wines

Written by Elijah Wines

Software Engineer - JavaScript, Ruby, React, Ruby on Rails

No responses yet

Write a response