Sort in STL C++

About Sort in STL

On this page we will discuss about library function sort() in STL which is used in C++. The Standard Template Library(STL) is a set of complete data structures and functions which can be used in C++. The sort() function in C++ is a function in the algorithm header that is used to sort elements in a range.

Sort in STL C++

Sort in STL in C++

  • In C++ programming language, the sort() function is included in algorithm header file.
  • Sorting is defined as arranging the data in a particular order, which can be increasing, decreasing or in any specific way.

Syntax

sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

where first and last are the beginning and ending iterators, respectively, of the range to be sorted and comp must be a function that should take two arguments of the same type as the elements in the range being sorted, and it should return a bool indicating whether the first argument should be considered “less than” the second argument.

Parameters

The sort() function in C++ has the following parameters:

ParameterDescription
firstIt is an iterator pointing to the first element in the range to be sorted.
lastIt is an iterator pointing to the element one past the last element in the range to be sorted.
compIt is an optional parameter that specifies a custom comparison function to be used to compare the elements in the range being sorted.

Sorting in Ascending Order :

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

int main() {
    vector< int > arr;
    arr.push_back(45);
    arr.push_back(80);
    arr.push_back(35);
    arr.push_back(69);
    arr.push_back(3);
    
     cout<<"The element of array before sorting : ";
    for(int i=0;i< arr.size();i++){
         cout<< arr[i]<<" ";
    }
    sort(arr.begin(), arr.end());
    cout<< endl;   
    cout<<"The element of array after sorting : ";
    for(int i=0;i< arr.size();i++){
         cout<< arr[i]<<" ";
    }
   
    return 0;
}

Output:

The element of array before sorting : 45 80 35 69 3 
The element of array after sorting : 3 35 45 69 80 
In the above program, we take the array and used sort function to sort the elements in ascending order as comparator is not used, by default elements are arranged in ascending order.

Sorting in Descending Order:

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

int main() {
    vector< int > arr;
    arr.push_back(45);
    arr.push_back(80);
    arr.push_back(35);
    arr.push_back(69); 
    arr.push_back(3);
    
     cout<<"The element of array before sorting : ";
    for(int i=0;i< arr.size();i++){
         cout<< arr[i]<<" ";
    }
    cout<< endl;
    
    // sort function for sorting the elements
    sort(arr.begin(), arr.end(), greater());  
    cout<<"The element of array after sorting : ";
    for(int i=0;i< arr.size();i++){
         cout<< arr[i]<<" ";
    }
   
    return 0;
}
    

Output:

The element of array before sorting : 45 80 35 69 3 
The element of array after sorting : 80 69 45 35 3 
In the above program, we take the array and used sort function to sort the elements in decreasing order as greater<int>() comparator is used to sort the elements in decreasing order

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription