~/coding-interview/bubble-sort-algorithm.md

Bubble Sort Algorithm

1 min readMay 16, 2019by Gorav Singal#coding-interview#sorting-algorithms#bubble-sort
// TL;DR

Compare adjacent pairs, swap if out of order, repeat until sorted. The largest element 'bubbles' to the end each pass. O(n²) time.

Bubble Sort Algorithm

This is kind of preliminary technique of sorting. And, this is the first algorithm that a beginner learns.

In this algorithm, we iterate over the array, and compare two consecutive numbers. And, if first is larger, then swap it. And, we keep on doing this till end. So, the Bubble here is the biggest element which we keep on swapping.

Bubble Sort Algorithm

  • We start with two loops. Outer loop just goes from 0 to last-1 index.
  • Inner loop always starts frmo 0-index, and goes till n-1 each time.
  • On first iteration, we got the biggest element at the end of the array.
  • Next iteration goes till n-1 elements. Since, we already sorted the largest element at the end.
  • We keep on pushing the largets element from remaining n-1 array.

See the code here:

public void sort(int[] arr) {
    int l = arr.length;
    for (int i=0; i<l-1; i++) {
        for (int j=0; j<(l-i-1); j++) {
            if (arr[j] > arr[j+1]) {
                //swap
                int t = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = t;
            }
        }
    }
}

Graphical Example

Bubble Sort Example

Key Points

  • Its an in-place sorting algorithm
  • Performance is usually worse than Insertion sort
  • Applicable for small set of input only
  • Its very simple algorithm

Runtime complexity

The algorithm runs on O(n^2) in worst/average case.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Radix Sort Algorithm

Radix Sort Algorithm

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

Counting Sort Algorithm

Counting Sort Algorithm

Counting sort runs on relatively smaller set of input. Counting sort calculates…

Heap Sort Algorithm

Heap Sort Algorithm

This is another very useful sorting algorithm based on Heap data structure. Read…

Quick Sort Algorithm

Quick Sort Algorithm

This algorithm is very useful for large input. And, is quite efficient one. It…

Merge Sort Algorithm

Merge Sort Algorithm

This algorithm is very efficient one, and is classic example of Divide and…

Selection Sort Algorithm

Selection Sort Algorithm

It is one of a simple algorithm to study for a beginner to understanding sorting…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…