# 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

### Insertion Sort Algorithm

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

### How to prepare for your next Coding Interview

Here are some tips while preparing for your coding interviews. 1. Do study or…

### Three Sum - Leet Code Solution

Problem Statement Given an array nums of n integers, are there elements a, b, c…

### Rotate Array - Leet Code Solution

Problem Statement Given an array, rotate the array to the right by k steps…

### Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

### Container with Most Water - Leet Code Solution

Problem Statement Given n non-negative integers a1, a2, …, an , where each…

## Latest Posts

### Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

### Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

### Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

### Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction So you have a Django project, and want to run it using docker image…

### Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

### Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…