Thursday, July 7, 2011

Sorting in C - Descending

Same as the previous program just to change the condition in the if the statement.

if(a[i]<=a[j])----->if(a[i]>=a[j])

You will get the output.

#include
#include
int main()
{
int i,j,temp;
int a[5]={-123,34,14,87,45};
printf("SORTING PROGRAM IN DESCENDING\n");
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
} for(i=0;i<5;i++)
printf("%d\n",a[i]);
getch();
}

No comments:

Post a Comment