EmbLogic's Blog

Bubble sorting using recursion

#include<stdio.h>
#include<stdlib.h>
int * input(int *,int);
int * sort(int *,int);
void display(int*,int);

int main()
{
int *a,n,i,j,c,temp;
printf(“Enter number of numbers you want to sort?\n”);
scanf(“%d”,&n);
a = input(a,n);
a = sort(a,n);
display(a,n);
}
int * input(int *a,int n)
{
int i;
printf(“Enter %d elements\n”,n);
a = (int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
return a;
}
void display(int*a,int n )
{
int i;
printf(“Sorted list\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
}

int * sort(int *a,int n)
{    int i,j,temp;
if(n==0)
return a;
for(i=0;i<n-1;i++)
if(*(a+i)>*(a+i+1))
{
temp = *(a+i);
*(a+i) = *(a+i+1);
*(a+i+1) = temp;
}
sort(a,n-1);
}

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>