BUBBLE SORT IN C PROGRAMMING:
Bubble sort is a simple sorting program which compares each and every element with its adjacent element in the given array and sorts them in ascending order by swapping if there are not in correct order.It will continue until no swaps found. It is named as bubble sort because as the smaller elements will bubble to the first.PROGRAM FOR BUBBLE SORT:
int main()
{
int a[50],n,i,j,temp;
printf("Enter the size of array: ");
scanf("%d",&n);
printf("Enter the array elements: ");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=1;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nArray after sorting: ");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
OUTPUT:
Enter the size of an array:5Enter the array elements:90 23 56 1 69
Array after sorting:1 23 56 69 90
0 Comments