EmbLogic's Blog

Bubble sorting using function

#include<stdio.h>
//Program for Bubble Sorting
int * bubblesort(int*,int);
int display(int *,int);
int main()
{
int i,j,a[100],N,temp;

printf(“Enter no.of elements you want to sort:\t”);
scanf(“%d”,&N);//Max. no. of elements
printf(“Enter %d elements you want to sort:\n”,N);
for(i=0;i<N;i++)
scanf(“%d”,(a+i));
printf(“\nBefore sorting:\t”);
display(a,N);
//Sorting Starts
bubblesort(a,N);
//Sorting Ends
printf(“\nAfter sorting:\t”);
display(a,N);
}

int * bubblesort(int *a,int N)
{
int i,j,temp;
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;
}
return a;

}
int display(int* a,int N)
{
int i;
for(i=0;i<N;i++)
printf(“%d\t”,*(a+i));

return 0;
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>