155. Design a function to print the elements of given integer array and arrange it ascending order.
#include<stdio.h>
int
array[5]={1,4,6,8,3},ele;
void
print_array(const int *,int ele);
void
bubble_sort(int *,int);
void
main()
{
ele=sizeof(array)/sizeof(array[0]);
print_array(array,ele);
printf("\n");
bubble_sort(array,ele);
print_array(array,ele);
printf("\n");
}
void
bubble_sort(int *a,int ele)
{
int i,j;
for(i=0;i<ele-1;i++)
{
for(j=0;j<ele-i-1;j++)
{
if(a[j]>a[j+1])
{
a[j]=a[j]^a[j+1];
a[j+1]=a[j]^a[j+1];
a[j]=a[j]^a[j+1];
}
}
printf("\n");
}
}
void
print_array(const int *p,int ele)
{
int i;
for(i=0;i<ele;i++)
{
//printf("%d ",*(p+i));
//printf("%d ",*p++);
printf("%d ",p[i]);
}
}
Comments
Post a Comment