Updated March 27, 2023
Introduction to Bubble Sort Algorithm
A bubble sort algorithm repeatedly swaps the adjacent elements if they are in the wrong order. The bubble sort is often used to implement a sorting algorithm. Every element in the Bubble is contrasted with its surrounding elements in Bubble form. The list will be processed through the algorithm. N-1 passes are necessary for sorting a list with n elements. Take a table A of n elements that have to be sorted with a sort of Bubble. The algorithm follows like
- In pass 1, the figures A[0] are composed of A[1], the figures A[1] are compared to A[2], the figures A[2] to A[3], etc. The largest item in the list is placed at the end of pass 1 at the list’s highest index.
- A[0] is compared to A[1] in pass 2, A[1] is compared to A[2], etc. The second-largest item is put on the second-highest index of the list at the end of Pass 2.
- The n-1 move compared A[0] to A[1], A[1] to A[2] and so on. The pass is over at the bottom. The first index of the list is the smallest item in the list.
Let us have a look at the complexity of Bubble Sort:
The scenario of Bubble Sort | The complexity of Bubble sort |
Space | 0(1) |
Worst-case running time | 0(n^2) |
Average Case Running time of Bubble | 0(n) |
Best Case Running time of Bubble | 0(n^2) |
Examples to Implement in Bubble Sort Algorithm
Let’s look at the program with different languages in order to understand it better :
Example #1 – First program in C language
Code:
#include<stdio.h>
void main ()
{
int a, m,temp;
int p[10] = { 55, 96, 61, 563, 4, 464, 112, 788, 934, 2};
for(a = 0; a<10; a++)
{
for(m = a+1; m<10; m++)
{
if(p[m] > p[a])
{
temp = p[a];
p[a] = p[m];
p[m] = temp;
}
}
}
printf("Code will print the list of items sorted ...\n");
for(a = 0; a<10; a++)
{
printf("%d\n",p[a]);
}
}
Output:
Example #2 – Program in Java language
Code:
public class BubbleSort {
public static void main(String[] args) {
int[] m = {55, 96, 61, 563, 4, 464, 112, 788, 934, 2};
for(int n=0;n<10;n++)
{
for (int j=0;j<10;j++)
{
if(m[n]<m[j])
{
int temp = m[n];
m[n]=m[j];
m[j] = temp;
}
}
}
System.out.println("Code will print the list of items sorted.");
for(int n=0;n<10;n++)
{
System.out.println(m[n]);
}
}
}
Output:
Example #3 – Program in JavaScript Language
Code:
<html>
<head>
<title>
Bubble Sort program in Javascript
</title>
</head>
<body>
<script>
var n = [55, 96, 61, 563, 4, 464, 112, 788, 934, 2];
for(m=0;m<10;m++)
{
for (j=0;j<10;j++)
{
if(n[m]<n[j])
{
temp = n[m];
n[m]=n[j];
n[j] = temp;
}
}
}
txt = "<br>";
document.writeln("Code will print the list of items sorted .."+txt);
for(m=0;m<10;m++)
{
document.writeln(n[m]);
document.writeln(txt);
}
</script>
</body>
</html>
Output:
Example #4 – Program in C++ Language
Code:
#include<iostream>
using namespace std;
int main ()
{
int m, j,temp;
int a[10] = { 55, 96, 61, 563, 4, 464, 112, 788, 934, 2};
for(m = 0; m<10; m++)
{
for(j = m+1; j<10; j++)
{
if(a[j] < a[m])
{
temp = a[m];
a[m] = a[j];
a[j] = temp;
}
}
}
cout <<"Code will print the list of items sorted ...\n";
for(m = 0; m<10; m++)
{
cout <<a[m]<<"\n";
}
return 0;
}
Output:
Example #5 – Program in C# Language
Code:
using System;
public class Program
{
public static void Main()
{
int m, j,temp;
int[] b = { 55, 96, 61, 563, 4, 464, 112, 788, 934, 2};
for(m = 0; m<10; m++)
{
for(j = m+1; j<10; j++)
{
if(b[j] > b[m])
{
temp = b[m];
b[m] = b[j];
b[j] = temp;
}
}
}
Console.WriteLine("Code will print the list of items sorted ..\n");
for(m = 0; m<10; m++)
{
Console.WriteLine(b[m]);
}
}
}
Output:
Example #6 – Program in PHP Language
Code:
<?php
$a = array(55, 96, 61, 563, 4, 464, 112, 788, 934, 2);
for($m=0;$m<10;$m++)
{
for ($j=0;$j<10;$j++)
{
if($a[$m]<$a[$j])
{
$temp = $a[$m];
$a[$m]=$a[$j];
$a[$j] = $temp;
}
}
}
echo "Code will print the list of items sorted ...\n";
for($m=0;$m<10;$m++)
{
echo $a[$m];
echo "\n";
}
?>
Output:
- Thus, Bubble Sort’s time complexity is O(n2). For Bubble Sort, the space complexity is O(1) since only one additional space, i.e. for the temp variable, is needed. In addition, O(n) is the best time complexity when the list has already been sorted.
Conclusion
This article has seen what bubble sort is and how it is implemented in various programming languages. I hope you will find this article helpful.
Recommended Articles
This is a guide to the Bubble Sort Algorithm. Here we discuss the introduction and top 6 different examples to implement with proper codes and examples. You can also go through our other related articles to learn more –