1
2// STL IN C++ FOR SORING
3#include <bits/stdc++.h>
4#include <iostream>
5using namespace std;
6int main()
7{
8 int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
9 int n = sizeof(arr)/sizeof(arr[0]);
10 sort(arr, arr+n); // ASCENDING SORT
11 reverse(arr,arr+n); //REVERESE ARRAY
12 sort(arr, arr + n, greater<int>());// DESCENDING SORT
13 }
1sort(arr, arr+length); //increase
2sort(arr, arr+length, greater<int>()); //decrease
1 int arr[]= {2,3,5,6,1,2,3,6,10,100,200,0,-10};
2 int n = sizeof(arr)/sizeof(int);
3 sort(arr,arr+n);
4
5 for(int i: arr)
6 {
7 cout << i << " ";
8 }
9
1// sort algorithm example
2#include <iostream> // std::cout
3#include <algorithm> // std::sort
4#include <vector> // std::vector
5
6bool myfunction (int i,int j) { return (i<j); }
7
8struct myclass {
9 bool operator() (int i,int j) { return (i<j);}
10} myobject;
11
12int main () {
13 int myints[] = {32,71,12,45,26,80,53,33};
14 std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
15
16 // using default comparison (operator <):
17 std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
18
19 // using function as comp
20 std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
21
22 // using object as comp
23 std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
24
25 // print out content:
26 std::cout << "myvector contains:";
27 for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
28 std::cout << ' ' << *it;
29 std::cout << '\n';
30
31 return 0;
32}