Tuesday, October 13, 2009

bubble with pointer

#include"stdio.h"
#include"string.h"
#include"conio.h"

/*12. Implement the bubble
* sort algorithm using pointer concept. */
#include"stdio.h"
#include"conio.h"

int i,j,n;
int bubble(int *a,int n);

int main()
{
int a[20];
printf("enter the limits\n");
scanf("%d",&n);

printf("\nenter the elements wants to sort\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
//a[i]=&t[i];
//a++;
}
// for(i=0;i<n;i++)
// printf("\n%d",a[i]);

bubble(a,n); //call function
getch();
return 0;
}


int bubble(int *a,int n)
{
int i,j,temp;

// for(i=0;i<n;i++)
// printf("\n%d",a[i]);

for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
// printf("%d",*(a+j));
}

}//printf("number of iteration %d",i+1);
}
printf("\n the sorted list is:\n");
for(j=0;j<n;j++)
{
printf("%d\n",a[j]);
//a++;
}
//getch();
return 0;
}

No comments:

Post a Comment