Friday, 1 April 2016

Sort an Array Elements in Descending Order in C++

Sort an Array Elements in Descending Order in C++

To write Sort an Array Elements in Descending Order in C++ code we need to follow bubble sort concept and swap all array elements.

Sort an Array Elements in Descending Order in C++

Code

#include<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
int a[20],i,j,temp;
cout<<"Enter Any 10 Elements:";
for(i=0;i<10;i++)
{
cin>>a[i];
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(i=0;i<10;i++)
cout<<"Elements in Decending Order:"
cout<<a[i]<<endl;
getch();
}

Output:

Enter any 10 number:
10
20
40
90
80
55
43
98
21
02
Elements in Accending Order:
98
90
80
55
43
40
21
20
10
02

No comments:

Post a Comment