DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

Sort Function in C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
int n ;
cin >> n ;
int a[n] ;
for(int i = 0 ; i < n ; i++){
    cin >> a[i] ;
}
//sort(starting ,ending) ;
sort(a+2 , a+8) ;
for(int i = 0 ; i < n ; i++){
cout << a[i] <<" " ;

}
}
Enter fullscreen mode Exit fullscreen mode

Input -
8
9 8 7 6 5 4 3 2
Output -
9 8 2 3 4 5 6 7

If we want to sort the full array . We will write the function -

sort(a,a+n) ;

Enter fullscreen mode Exit fullscreen mode

Input -
8
9 8 7 6 5 4 3 2
Output -
2 3 4 5 6 7 8 9
This is ascending order sort .
To sort in descending order we will write -

`sort(a,a+n,greater<int>()) ;
`
Enter fullscreen mode Exit fullscreen mode

Input -
10
9 8 7 6 5 4 3 2 2 1
Output -
9 8 7 6 5 4 3 2 2 1

Top comments (0)