Monday, November 2, 2015

Sorting Given Number In Ascending Order - Working Code

#include<stdio.h>
#include<conio.h>
void main()
{
   int i,j,num,temp,arr[20];
   clrscr();
   printf("Enter The Total Number Of Values For Sorting: \n");
   scanf("%d",&num);
   for(i=0;i<num;++i)
   {
printf("Enter Number %d: \n",i+1);
scanf("%d",&arr[i]);
   }

   for(i=1;i<num;i++)
   {
for(j=0;j<num;j++)
{
    if(arr[i]<arr[j])
    {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
    }
}
   }
   printf("The Given Number In Ascending Order Are: \n");
   for(i=0;i<num;i++)
   {
       printf("%d\n",arr[i]);
   }
   getch();
}

To Compile Press Alt+f9 
To Run Press Ctrl+f9

Note: For sorting descending number, change arr[i]<arr[j] to arr[i]>arr[j]

Output:


No comments:

Post a Comment