Program
#include<stdio.h> //program to sort array using pointer
void swap(int *px,int *py)
{
int temp;
temp=*px;
*px=*py;
*py=temp;
}
main()
{
int i,j;
int arr[]={45,40,78,69,55,12,5,2};
int *ptr=arr;
printf("Before Sorting array=\n");
for(i=0;i<8;i++)
{
printf("%d\t",*(ptr+i));
}
for(i=0;i<7;i++)
{
for(j=0;j<7-i;j++)
{
if(*(ptr+j)>*(ptr+j+1))
swap((ptr+j),(ptr+j+1));
}
}
printf("\nAfter Sorting array=\n");
for(i=0;i<8;i++)
{
printf("%d\t",*(ptr+i));
}
}
Output
Before Sorting array=
45 40 78 69 55 12 5 2
After Sorting array=
2 5 12 40 45 55 69 78
#include<stdio.h> //program to sort array using pointer
void swap(int *px,int *py)
{
int temp;
temp=*px;
*px=*py;
*py=temp;
}
main()
{
int i,j;
int arr[]={45,40,78,69,55,12,5,2};
int *ptr=arr;
printf("Before Sorting array=\n");
for(i=0;i<8;i++)
{
printf("%d\t",*(ptr+i));
}
for(i=0;i<7;i++)
{
for(j=0;j<7-i;j++)
{
if(*(ptr+j)>*(ptr+j+1))
swap((ptr+j),(ptr+j+1));
}
}
printf("\nAfter Sorting array=\n");
for(i=0;i<8;i++)
{
printf("%d\t",*(ptr+i));
}
}
Output
Before Sorting array=
45 40 78 69 55 12 5 2
After Sorting array=
2 5 12 40 45 55 69 78