Source code:
/*Write a C program which will sort number of elements in an ascending by using bubble sort. We will rewrite the algorithm by using pointer. Read 5 elements and sort in ascending order. */
#include<stdio.h>
void bubble(int *ptr,int s)
{
int i,j;
int temp;
for(i=1;i<s;i++)
{
for(j=0;j<s-i;j++)
{
if(*(ptr+j)>*(ptr+j+1))
{
temp=*(ptr+j);
*(ptr+j)=*(ptr+j+1);
*(ptr+j+1)=temp;
}
}
}
}
int main()
{
int arr[10];
int i;
int size=5;
/* Read Array Elements */
printf("Enter 10 elements: ");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
bubble(arr,size);
printf("The sorted elements:\n");
for(i=0;i<size;i++)
printf("%d\n",arr[i]);
return 0;
}
No comments:
Post a Comment