coding-interview|May 16, 2019|1 min read

Bubble Sort Algorithm

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.

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

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…