Learn How Buuble Sort Method Works

Algorithm of Bubble Sort



Step 1: Repeat up to Step3 for I = 0 to N-1
Step 2: Repeat Step3 for J = 0 to N – I – 1
Step 3: If a [J] > a [J + 1] then
TEMP = a [J]
a [J] = a [J + 1]
a [J + 1] = TEMP

     

Program of Bubble Sort


#include<stdio.h>
#include<conio.h>
#define N 5
void main()
{
void bubble(int *a);
int a[N],i;
clrscr();
printf("Enter Elements in array\n");
for(i=0;i<N;i++)
{
printf("Enter Value of A[%d]:",i);
scanf("%d",&a[i]);
}
bubble(a);
printf("Sorted List\n");
for(i=0;i<N;i++)
{
printf("A[%d]=%d\n",i,a[i]);
}
getch();
}
void bubble(int *a)
{
int i,j,temp;
for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

Download Projects


Download Programs