Bubble Sort is the most simple form of sorting algorithm based on comparisons. It works by repeatedly stepping through the list of items such as an array and swapping the two adjacent elements if they are in a different order.
This algorithm has no such real-life uses due to its poor performance and is used primarily as an educational tool to teach students about sorting concepts.
Take a look at this video explaining Bubble Sort Algorithm in two minutes.
Bubble Sort Algorithm Performance
Bubble sort algorithm is the worst performing algorithm and most of the other sorting algorithms have a better worst-case or average complexity, often O(n log n).
Worst Case and Average Case Complexity: O(n2) Best Case Time Complexity: O(n)
Bubble Sort Comparison with other sorting algorithms
Bubble Sort – Pseudocode
Bubble Sort Program
The following C program reads an integer array and prints it on the screen. It then sorts the array using bubble sort and print it again.
Unlock the world of programming with C++ for Dummies – the perfect beginner’s guide to mastering C++ with ease and confidence! View on Amazon
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#define NMAX 10
intgetIntArray(inta[],intnmax,intsentinel);
voidprintIntArray(inta[],intn);
voidbubbleSort(inta[],intn);
intmain(void){
intx[NMAX];
inthmny;
intwho;
intwhere;
hmny=getIntArray(x,NMAX,0);
if(hmny==0)
printf("This is the empty array!\n");
else{
printf("The array was: \n");
printIntArray(x,hmny);
bubbleSort(x,hmny);
printf("The sorted array is: \n");
printIntArray(x,hmny);
}
}
voidprintIntArray(inta[],intn)
{
inti;
for(i=0;i<n;){
printf("\t%d ",a[i++]);
if(i%5==0)
printf("\n");
}
printf("\n");
}
intgetIntArray(inta[],intnmax,intsentinel)
{
intn=0;
inttemp;
do{
printf("Enter integer [%d to terminate] : ",sentinel);
Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.