Tuesday, 9 December 2014

C program to Sort an array without using strcmp

Sorting an array without using strcmp

#include<stdio.h>
#include<string.h>
main()
{
int a[5],i,j,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter an array of 5 elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("array after sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
OutPut:
enter an array of 5 elements:
43
32
33
12
22
array before sorting
43 32 33 12 22 array after sorting
12 22 32 33 43