Monday, 21 March 2016

Bubble Sort Program in C

Bubble Sort Program in C

Today we write Bubble Sort Program in C. Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Bubble Sort Program in C

Using this technique you can sort an array element in ascending or descending order according to your requirement.

Code

#include<stdio.h>
#include<conio.h>
int main()
{
int size,temp,i,j,arr[20];
printf("Enter size of array: ");
scanf("%d",&size);
printf("Enter %d elements in array: ",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<size;i++)
printf(" %d",arr[i]);
return 0;
}

Output:

Enter size of array: 5
Enter 5 elements in array: 10 12 20 1 6
After sorting: 1 6 10 12 20

No comments:

Post a Comment