c program to sort element of one dimensional array in ascending or descending order

c program to sort element of one dimensional array in ascending or descending order


Write a program  to sort element of one dimensional array in ascending or descending order

Ascending

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,temp,a[1000];
printf("Enter the number of elements in the array:");
scanf("%d",&n);

for(i=0; i<n; i++){
printf("Enter the %d elements of array:",i);
scanf("%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("The sorted list in ascending order is:");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
getch();
return 0;
}

Descending

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,temp,a[1000];
printf("Enter the number of elements in the array:");
scanf("%d",&n);

for(i=0; i<n; i++){
printf("Enter the %d elements of array:",i);
scanf("%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("The sorted list in descending order is:");
for(i=0; i<n; i++)
printf("%d\n",a[i]);
getch();
return 0;
}


You can Browse related article below for more information and program code related to recursive function call

 Does above is helpful , Post you views in comment

DO NOT MISS OTHER C PROGRAMMING TUTORIAL

* indicates required



Comments

Popular posts from this blog

Copy Constructor

Print pattern in c - explore new perspectives in pattern printing

C program to check if the supplied year is leap year or not.