# Sorting Array Elements as Ascending and Descending Order in C++

यहाँ पर दो program दिए गए हैं-

## C++ Sorting Array Element as Ascending Order

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int arr[10],size,i;

cout<<"Enter the size of Array: "; cin>>size;

cout<<"Enter the element in the Array\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}

// display entered element
cout<<"Before Sorting Entered Element Are\n";
for(i=0;i<size;i++)
cout<<arr[i]<<"\t";

// sorting process begin
int temp;  // declare third variable for swapping

for(i=0;i<size;i++)   //outer-loop
{
for(int j=0;j<=size;j++)  //inner-loop
{
if(arr[i]<arr[j]) // represent second element in the array list
{
temp = arr[i];    // first array element assign to variable temp
arr[i] = arr[j]; // second element assigning to first element
arr[j] = temp; // variable temp (means first element) assigning to second element
}
}
}

// display sorting element
cout<<"\nAfter Sorting Entered Element Are\n";

for(int i=0;i<size;i++)
cout<<arr[i]<<" ";

getch();
}

OUTPUT

Enter the size of Array:5

Enter the element in the Array
8
7
2
1
4

Before Sorting Entered Element Are
8       7       2       1       4

After Sorting Entered Element Are
1       2       4       7       8

## C++ Sorting Array Element in Descending Order

अवरोही क्रम में चढ़ते हुए हम केवल ऑपरेटर बदलेंगे-

for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(arr[i]>arr[j]) // arr[i+1] represent second element in the array list
{
temp = arr[i];    // first array element assign to variable temp
arr[i] = arr[j]; // second element assigning to first element
arr[j] = temp; // variable temp (means first element) assigning to second element
}
}
}

Related exercise: