Home » NO IDEA » Bubble Sort

Bubble Sort

Blog Stats

  • 15,880 hits
December 2013
M T W T F S S
« Oct   Jan »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Bubble sorting is the one of the simple sorting algorithm .Bubble sort got its name ,all the least element go to top of the list so it is called as bubble sort

Bubble sort works by checking the fist element with second element if the element is greater ,then swap the value else remains unaffected .This process is repeated till the last element

Graphical Representation  

Bubble-sort












#include<stdio.h>
int bubble(int A[],int n);
int bubble(int A[],int n)
{
int pass,i;
for( pass=0;pass<n;pass++)
{
for( i=0;i<n;i++)
{
if(A[i]>A[i+1])
{
int t=A[i];
A[i]=A[i+1];
A[i+1]=t;
}
}
printf("\n pass %d \n ",pass);

for(i=0;i<6;i++)
{
printf("%d\n",A[i]);
}
}
printf("\n the answer is \n");
for(i=0;i<6;i++)
{
printf("%d\n",A[i]);
}
return 0;
}

int main()
{
int m=6;
int a[6]={15,2,78,4,1,68};
bubble(a,m);
return 0;
}

Advantages :
Very simple
Can be used to check whether the given array is sorted or not easily

Disadvantages:

more time complexity
not very efficient one

Time Complexity :
Best case O(n^2) (modified version has O(n)
Worst case and Average case O(n^2)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: