Tuesday, 17 December 2019

Bubble sort in C

/*Program to sort a given array using bubble sort*/

#include<malloc.h>
#include<stdio.h>
#include<conio.h>
main()
{
int j,i,n,*arr,temp;
printf("\n Enter the number of elements ");
scanf("%d",&n);
arr=(int*)malloc(n*2);
for(i=0;i<n;i++)
{
 printf("\n Enter the element %d ",i);
 scanf("%d",(arr+i));
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(*(arr+j)>*(arr+j+1))
{
temp=*(arr+j);
*(arr+j)=*(arr+j+1);
arr[j+1]=temp;
}
}
}
printf("\n The sorted array is: ");
for(i=0;i<n;i++)
printf("\t %d ",*(arr+i));
getch();
}

No comments:

Post a Comment

Convey your thoughts to authors.