Displaying Source Code(s)
|
|
Prg. to sort any no. of numeral i/p in ascending or descending
order.
--------------------------------------------------------------------------------
void sort(void);
int c,a[20],l;
void main()
{
clrscr();
printf("Enter no. of elements in the list:- ");
scanf ("%d",&l);
printf("
CHOICE:-");
printf("
(1) Sort in ascending order.");
printf("
(2) Sort in descending order.");
printf("
CHOICE:- ");
scanf("%d",&c);
if (c!=1 && c!=2)
{
printf("
ERROR");
getch();
exit(0);
}
sort();
getch();
}
void sort(void)
{
int n,i,j,temp=0,min,k;
for (i=1;i<=l;i++)
{
printf("
Enter no.:- ");
scanf("%d",&a[i]);
}
for (i=1;i<=(l-1);i++)
{
min=a[i];
k=i;
for (j=(i+1);j<=l;j++)
{
if (a[j]<min)
{
min=a[j];
k=j;
}
}
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
switch(c)
{
case 1:
printf("
Elements in ascending order are:-");
for (i=1;i<=l;i++)
printf("
%d",a[i]);
break;
case 2:
printf("
Elements in descending order are:-");
for (i=l;i>=1;i--)
printf("
%d",a[i]);
break;
default:
printf("
ERROR");
}
return;
}
|
|
|