# Bubble Sort

Bubble Sort is a basic sorting algorithm for organizing an array of elements into a certain order. This algorithm is a popular educational tool for programmers.

Product managers don’t necessarily need to know advanced software development. However, they can still benefit from an understanding of various algorithms beyond just deploying others’ pre-made functions and libraries. Bubble sort is a basic programming concept that can be useful for anyone to know regardless of whether or not you have a programming background.

In this article, we’ll define the bubble sort algorithm and also its best use cases.

## Table of contents

## What is Bubble Sort?

Bubble sort is part of a class of algorithms called ‘sorting algorithms.’ These algorithms are designed to place a collection or array of elements in order. Each of these algorithms varies in complexity, but bubble sort is one of the simplest to understand.

## How Do You Implement Bubble Sort?

To understand bubble sort, let’s say you have a list of random numbers. When you use bubble sort, the algorithm starts at the first number in the list and then compares it to the next number. If the first number is larger (or smaller, depending on the desired sorting order) than the next number, then it swaps the two.

If there is no difference between them, then the algorithm leaves the two numbers alone and moves on to the next number, where it performs another comparison between number pairs.

After the algorithm has gone through the entire list, it will perform another iteration that once again traverses through the list and compares adjacent number pairs. It cycles through the entire array until it completes an entire iteration without needing to make a swap. As it performs these iterations, larger or smaller numbers “bubble” to the top of the list, hence the algorithm’s name.

### Standard Example

Consider an array to be arranged in ascending order:

6 | 3 | 2 | 7 |
---|

During the first pass, the following operations will occur:

- 6 and 3 will be swapped
- 6 and 2 will be swapped

This results in the following array:

3 | 2 | 6 | 7 |

In the second pass, this will happen:

- 3 and 2 will be swapped

Creating the array:

2 | 3 | 6 | 7 |

The result is a fully sorted array in two passes.

### Worst-case Scenario Example

In the worst-case scenario, where an array is sorted in fully reversed order:

7 | 6 | 5 | 4 |

- 7 and 6 will be swapped
- 7 and 5 will be swapped
- 7 and 4 will be swapped

6 | 5 | 4 | 7 |

- 6 and 5 will be swapped
- 6 and 4 will be swapped

5 | 4 | 6 | 7 |

- 5 and 4 will be swapped

4 | 5 | 6 | 7 |

As you can see, it takes four iterations to sort this array.

## When Do You Use Bubble Sort?

Whale bubble sort is simple to use, it’s also quite slow. The larger the complexity, the less efficient the algorithm becomes. Because it has a time complexity of O(n^2), that means that the time it takes to complete it exponentially increases the more items you need to sort.

So, to get around this programmers have developed plenty of faster and better algorithms that are appropriate for larger arrays of elements. However, bubble sort does have certain uses that are still valid.

Here are a few ways to use bubble sort:

### 1. Sorting Small Data Lists

Due to its O(n^2) complexity, the time needed to complete a bubble sort rapidly grows with very large arrays. However, if you simply need to sort a shortlist once without needing to perform it often, or in real-time, a bubble sort is easy to implement.

### 2. Teaching Programming

Since bubble sort is intuitive and only uses basic tools and concepts in programming—string traversal, recursion, and comparison of two numbers—it’s often used for teaching purposes. Traditionally, it’s one of the first algorithms people familiarize themselves with in programming classes. If you need to demonstrate algorithms and programming concepts to someone, bubble sort is a great introduction.

### 3. Precisely Checking Whether a Data Set is Sorted

While bubble sort is inefficient, it’s also thorough*. *It visits the data set several times, checking whether any elements are out of place. Running an iteration of bubble sort can, therefore, help you definitively determine whether a data set is sorted.

### 4. Prioritizing Tasks

A large component of product management involves prioritizing tasks and initiatives. Many prioritization frameworks involve numerical scoring models, which assign values to each task. In order to sort these tasks by priority value, you can use a simple implementation to bubble up the most urgent and relevant tasks to the surface.

## Bubble Sort:** **When Simplicity is the Key

Bubble sort doesn’t often find usage in large applications due to its weaknesses and inefficiencies. However, the value of its simplicity and ease can’t be denied. As a tool, it’s a useful way to introduce concepts from computer science or programming to beginners. Plus, the quick implementation can be a lifesaver when you don’t have access to more complex sorting algorithms.