# C++ program to sort an array in ascending and descending order

## Sorting of array in ascending and descending order

There are numerous Algorithms to sort an array. Here we are using bubble sort Algorithm which takes O(n^2) time .
For Example:
Input :-    From the user {4,3,6,1,8}
Output :- In ascending order {1,3,4,6,8} and In descending order {8,6,4,3,1}. ## Algorithm:-

1. Input size of array.
2. Store it in some variable say n and a[].
3. To select each element from array, run an outer loop from 0 to n-1.
4. Run another inner loop from i + 1 to n – 1 to place newly selected element at its correct position.
5. Inside inner loop to compare currently selected element with subsequent element and swap two array elements, if not placed at its correct position.

## C++ Code:-

//C++ Program to sort an array in ascending or descending order
#include <iostream>
using namespace std;
void  main()
{
int i,j,n,a,temp,p,q,temp1;
cout<<“Enter the size of array : \n ;  //Taking size of array
cin>>n;
cout<<“Enter the elements : \n ;  //Taking input from user
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)  // loop for sorting array in ascending order
{
for(j=i+1;j<n;j++) {
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<“Ascending order of an array : \n;  //print ascending order
for(i=0;i<n;i++)
{
cout<<a[i]<<” “ ;
}

for(p=0;p<n;p++) // loop for sorting array in descending order
{
for(q=p+1;q<n;q++)
{
if(a[p]<a[q])
{
temp1=a[p];
a[p]=a[q];
a[q]=temp1;
}
}
}
cout<<\n Descending order of an array : \n;  // print descending order
for(p=0;p<n;p++)
{
cout<<a[p]<<” “;
}
}

Output:-
Enter the size of array :
5
Enter the elements :
9
5
1
2
7
Ascending order of an array :
1 2 5 7 9
Descending order of an array :
9 7 5 2 1

## Important Codes related to Arrays

• Find Smallest Element in an Array : C | C++ | Java | Python
• Find Second Smallest Element in an Array : C | C++ | Java | Python
• Find Largest element in an array : C | C++ | Java | Python
• Find the Smallest and largest element in an array : C | C++ | Java | Python
• Calculate the sum of elements in an array : C | C++ | Java | Python
• Reverse an Array : C | C++ | Java | Python
• Sort first half in ascending order and second half in descending : C | C++ | Java | Python
• Sort the elements of an array : C | C++ | Java | Python
• Finding the frequency of elements in an array : C | C++ | Java | Python
• Finding the Longest Palindrome in an Array : C | C++ | Java| Python
• Counting Distinct Elements in an Array : C | C++ | Java| Python
• Finding  Repeating elements in an Array : C | C++ | Java | Python
• Finding Non Repeating elements in an Array : C | C++ | Java | Python
• Removing Duplicate elements from an array : C | C++ | Java
• Finding Minimum scalar product of two vectors : C | C++ | Java | Python
• Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
• Counting the number of even and odd elements in an array : C | C++ | Java
• Find all Symmetric pairs in an array : C | C++ | Java
• Find maximum product sub-array in a given array : C | C++ | Java
• Finding Arrays are disjoint or not : C | C++ | Java
• Determine Array is a subset of another array or not : C | C++ | Java
• Determine can all numbers of an array be made equal : C | C++ | Java
• Finding Minimum sum of absolute difference of given array : C | C++ | Java
• Sorting elements of an array by frequency : C | C++ | Java
• Sort an array according to the order defined by another array : C | C++ | Java
• Replace each element of the array by its rank in the array : C | C++ | Java
• Finding equilibrium index of an array : C | C++ | Java| Python
• Rotation of elements of array- left and right : C | C++ | Java| Python
• Block swap algorithm for array rotation : C | C++ | Java| Python
• Juggling algorithm for array rotation : C | C++ | Java | Python
• Finding Circular rotation of an array by K positions : C | C++ | Java | Python

### You can find the below Programs in all languages 