# Bubble Sort Algorithm

May 16, 2019

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;
}
}
}
}``````

## 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.

## Similar Posts

### First Unique Character in a String - Leet Code Solution

Problem Statement Given a string, find the first non-repeating character in it…

### Insertion Sort Algorithm

Its a kind of incremental insertion technique, where the algorithm build up…

### Max Priority Queue Implementation with Heap Data structure

Max Priority Queue is a data structure which manage a list of keys(values). And…

System design interview is pretty common these days, specially if you are having…

### Counting Sort Algorithm

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

### Swap Nodes Pairs in Link List - Leet Code Solution

Problem Statement Given a linked list, swap every two adjacent nodes and return…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…