DEV Community

Fabrizio Bagalà
Fabrizio Bagalà

Posted on • Updated on

Bubble Sort

Sorting algorithms are fundamental components of computer science. They organize an array of data into a particular order, facilitating quicker and more effective data processing. This can be critical in many areas, from database operations to machine learning algorithms.

Among the wide array of sorting algorithms, Bubble Sort holds a unique position due to its simplicity and ease of understanding.

It is a straightforward sorting algorithm that operates by repeatedly comparing pairs of adjacent elements and swapping them if they are in the wrong order. This process continues throughout the entire list until no more swaps are necessary, indicating that the list is fully sorted.

A distinctive characteristic of Bubble Sort is the way it “bubbles” the larger elements toward the end of the array, which is how it earned its name.

How it works

Imagine having a list of numbers. The algorithm starts at the beginning of the list and compares the first two numbers. If the first number is bigger than the second, it swaps them. Then it moves to the next number and does the same thing, and continues to do so until it reaches the end of the list. This completes a single pass through the list.

After the first pass, the largest number will have "bubbled" to the end of the list. The process is then repeated for the rest of the list, except for the last element, then the second last, and so on, until you get to the start of the list.

Here is a simple implementation in C#:

public void BubbleSort(int[] arr)
{
    var n = arr.Length - 1;

    for (var i = 0; i < n; i++)
    {
        for (var j = 0; j < n - i; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Time complexity

It has a time complexity of O(n²) in both the worst-case and average-case scenarios, where n is the number of elements to be sorted. This means that the execution time for the algorithm increases quadratically with the size of the input.

Even in the best-case scenario, when the list is already sorted, Bubble Sort still has a time complexity of O(n). This is because the algorithm doesn't recognize the sorted state after the first pass and has to perform all the passes.

Space complexity

Since this is a stable in-place sorting algorithm, it needs only a constant amount of additional space to perform the sorting O(1). Sorting is performed directly on the input data structure, which makes it space-efficient.

Conclusion

Despite its limitations, Bubble Sort holds a special place in the vast realm of sorting algorithms. Its simplicity makes it an ideal starting point for learning about sorting algorithms, and its unique "bubbling" mechanism provides valuable insight into how data can be manipulated.

While it may not be the optimal choice for large datasets or time-critical applications, it is perfect for small or nearly sorted datasets. Moreover, its concept has been foundational in the development of more advanced sorting algorithms.

In the world of computer science education, Bubble Sort continues to "bubble up" fostering new generations of learners to explore, understand, and innovate.

References

  • Algoritmi e programmazione in C di Francesco Oliveri, Aracne editrice (Italian book)

Top comments (6)

Collapse
 
xaberue profile image
Xavier Abelaira Rueda

Great article @fabriziobagala !

Are you planning to do more posts about algorithms?

Many thanks!

Collapse
 
fabriziobagala profile image
Fabrizio Bagalà

Thank you Xavier 😄

Yes, I plan to create a series called "Sorting Algorithms" 😉

Collapse
 
ant_f_dev profile image
Anthony Fung

Very cool - can you give us a preview of what you'll be covering?

Thread Thread
 
fabriziobagala profile image
Fabrizio Bagalà

I would like to cover algorithms such as Selection Sort, Insertion Sort, Merge Sort, Quick Sort, and some less common sorting algorithms than those mentioned. Also, I am considering whether to review software that I use to develop and in my digital life. What do you think?

Thread Thread
 
ant_f_dev profile image
Anthony Fung

Sounds good to me.

Back when I was studying algorithms, Bubble sort was the only one I managed to grasp. It'd be great to revisit them!

Also, a software review/discussion sounds good. We all (probably) use different software and it'd be great to share our experiences. That way, everyone wins!

Thread Thread
 
fabriziobagala profile image
Fabrizio Bagalà

At the university, I studied data structures and algorithms and found it a very fascinating subject. Over the years, I also learned a few more algorithms. Now I decided to share my knowledge, trying to create content that is comprehensive but easy to read.

As for software, I will try to review/suggest/discuss software that I use and find interesting.

I haven't said it, but if there are suggestions on how to improve my content or there are requests for topics that I may know about, I am willing to listen and make articles.