void main()
{
int i,*a,n;
clrscr();
printf("\n Enter number of element you want:");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("\n Enter element[%d]:",i);
scanf("%d",&a[i]);
}
printf("\n------------------------------------------");
printf("\n Elements before sorting:");
display(a,n);
printf("\n------------------------------------------");
bubble_sort(a,n);
printf("\n------------------------------------------");
printf("\n Elements after sorting:");
display(a,n);
printf("\n------------------------------------------");
getch();
}
void bubble_sort(int *a,int n)
{
int pass,i,temp;
for(pass=0;pass<= n-1;pass++)
{
for(i=0;i< n-pass-1;i++)
{
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
printf("\n Pass %d:",pass+1);
display(a,n);
}
}
void display(int *a,int n)
{
int i;
for(i=0;i < n;i++)
{
printf(" %d ",a[i]);
}
}
OUTPUT
Enter number of element you want:9
Enter element[0]:9
Enter element[1]:8
Enter element[2]:7
Enter element[3]:6
Enter element[4]:5
Enter element[5]:4
Enter element[6]:3
Enter element[7]:2
Enter element[8]:1
------------------------------------------
Elements before sorting: 9 8 7 6 5 4 3 2 1
------------------------------------------
Pass 1: 8 7 6 5 4 3 2 1 9
Pass 2: 7 6 5 4 3 2 1 8 9
Pass 3: 6 5 4 3 2 1 7 8 9
Pass 4: 5 4 3 2 1 6 7 8 9
Pass 5: 4 3 2 1 5 6 7 8 9
Pass 6: 3 2 1 4 5 6 7 8 9
Pass 7: 2 1 3 4 5 6 7 8 9
Pass 8: 1 2 3 4 5 6 7 8 9
Pass 9: 1 2 3 4 5 6 7 8 9
------------------------------------------
Elements after sorting: 1 2 3 4 5 6 7 8 9
------------------------------------------
{
int i,*a,n;
clrscr();
printf("\n Enter number of element you want:");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("\n Enter element[%d]:",i);
scanf("%d",&a[i]);
}
printf("\n------------------------------------------");
printf("\n Elements before sorting:");
display(a,n);
printf("\n------------------------------------------");
bubble_sort(a,n);
printf("\n------------------------------------------");
printf("\n Elements after sorting:");
display(a,n);
printf("\n------------------------------------------");
getch();
}
void bubble_sort(int *a,int n)
{
int pass,i,temp;
for(pass=0;pass<= n-1;pass++)
{
for(i=0;i< n-pass-1;i++)
{
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
printf("\n Pass %d:",pass+1);
display(a,n);
}
}
void display(int *a,int n)
{
int i;
for(i=0;i < n;i++)
{
printf(" %d ",a[i]);
}
}
OUTPUT
Enter number of element you want:9
Enter element[0]:9
Enter element[1]:8
Enter element[2]:7
Enter element[3]:6
Enter element[4]:5
Enter element[5]:4
Enter element[6]:3
Enter element[7]:2
Enter element[8]:1
------------------------------------------
Elements before sorting: 9 8 7 6 5 4 3 2 1
------------------------------------------
Pass 1: 8 7 6 5 4 3 2 1 9
Pass 2: 7 6 5 4 3 2 1 8 9
Pass 3: 6 5 4 3 2 1 7 8 9
Pass 4: 5 4 3 2 1 6 7 8 9
Pass 5: 4 3 2 1 5 6 7 8 9
Pass 6: 3 2 1 4 5 6 7 8 9
Pass 7: 2 1 3 4 5 6 7 8 9
Pass 8: 1 2 3 4 5 6 7 8 9
Pass 9: 1 2 3 4 5 6 7 8 9
------------------------------------------
Elements after sorting: 1 2 3 4 5 6 7 8 9
------------------------------------------