The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Bubble Sort

Bubble sort is a very simple sorting algorithm of all sorting method. In bubble sort algorithm, sorting is done by swapping two number. The name implies, that each time a greater element is bubbled to the top of the array list.

Advantages - Bubble Sort

  • Bubble sort algorithm is very simple to understand.

Disadvantages - Bubble Sort

  • The complexity of any sorting algorithm depends upon the number of comparisons. In bubble sort algorithm, comparisons can be done at highest possibility, thus bubble sort algorithm is not suitable for array that contains huge amount of data.
  • The Executing time of bubble sort algorithm is 0 (n2). Where n is the total number of elements in the array.

C program - Bubble Sort

Here is the program to demonstrate Bubble Sort.

bubble-sort.c
#include <stdio.h>
int n, x[30], i, j;
int main()
{
void sort();
void display();
int i;
printf("\n Enter the size of the array : ");
scanf("%d", &n);
printf("\nEnter %d elements in the array : \n",n);
for(i=0; i < n; i++)
scanf("%d",&x[i]);
sort();
display();
return 0;
}
void display()
{
printf("\nSorted Array in ascending order is : \n");
for(i=0; i < n; ++i)
printf("%5d",x[i]);
}
void sort()
{
int swap = 1,temp;
for(i = 0; i < n && swap == 1; ++i)
{
swap = 0;
for(j = 0; j < n-(i+1); ++j)
if(x[j] > x[j+1])
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
swap = 1;
}
}
}
Enter the size of the array : 5 
Enter 5 elements in the array :
4 3 1 2 5 
Sorted Array in ascending order is : 
1 2 3 4 5

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report