Bubble Sort
#include<stdio.h>
#include<conio.h>
//To define function
void bubblesort(int a[],int n);
void main()
{
int i,a[100],n;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter element %d: ",i+1);
scanf("%d",&a[i]);
}
bubblesort(a,n);
getch();
}
//To sort the elements
void bubblesort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted Array is: \n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
0 Comments