PROGRAM TO SORT A GIVEN ARRAY USING BUBBLE SORT

/*PROGRAM TO SORT A GIVEN ARRAY USING BUBBLE SORT*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[10],temp;
clrscr();
printf(“Enter the size of array: “);
scanf(“%d”,&n);
printf(“Enter %d elements in the array:\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Array elements before sorting are:\n”);
for(i=0;i<n;i++)
printf(“%5d”,a[i]);
/*SORTING STARTS*/
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;
}
}

}
printf(“\nElements after sorting are:\n”);
for(i=0;i<n;i++)
printf(“%5d”,a[i]);
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *