array sort in cpp code example

Example 1: stl for sorting IN C++

// STL IN C++ FOR SORING
#include  
#include  
using namespace std; 
int main() 
{ 
    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
    sort(arr, arr+n);  // ASCENDING SORT
    reverse(arr,arr+n);   //REVERESE ARRAY 
    sort(arr, arr + n, greater());// DESCENDING SORT
  }

Example 2: how to sort an array in c++

#include  
using namespace std; 

#define size(arr) sizeof(arr)/sizeof(arr[0]);


int main(){

    int a[5] = {5, 2, 6,3 ,5};
    int n = size(a);
    sort((a), a + n);
    for(int i = 0; i < n; i++){
        cout << a[i];
    }


    return 0;

}

Example 3: stl sort in c++

sort(arr, arr+n); // sorts in ascending order

Tags:

Misc Example