Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sort function in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 268
    Comment on it

    Sort function is used to sort a given range in ascending order. This function is defined in the header algorithm. There are two version are defined of it.

    template< class RandomIt >
    void sort( RandomIt first, RandomIt last );
    
    template< class RandomIt, class Compare >
    void sort( RandomIt first, RandomIt last, Compare comp );
    

    The first version sorts the range in ascending order. Second version uses comparison function comp in which sort function works according to comp function.

    About the parameters

    first
    It is the initial position of the range which is included in the range.

    last
    It is the position of the range which is excluded in the range.

    comp
    It is the comparison function, it returns a value convertible to bool.

    Sample program using the first version

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    
    using namespace std;
    
    int main()
    {
            vector <int> v;
            vector<int> :: iterator it;
    
            for (int i = 10; i >= 0; i--) {
                    v.push_back(i);
            }
    
            //Before sort printing the elements of vector
            for (it = v.begin(); it != v.end(); it++) {
                    cout << *it << " ";
            }
            cout << endl;
    
            sort(v.begin(),v.end()); //applying first version of sort (the default sort)
    
            //After sort printing the elements of vector
            for (it = v.begin(); it != v.end(); it++) {
                    cout << *it << " ";
            }
            cout << endl;
    
            return 0;
    }
    

    Output of the above program

    Before applying Sort Function
    Output : 10 9 8 7 6 5 4 3 2 1 0 
    After applying Sort Function
    Output : 0 1 2 3 4 5 6 7 8 9 10
    

    Sample program using the second version

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    
    using namespace std;
    
    int main()
    {
            vector <int> v;
            vector<int> :: iterator it;
    
            for (int i = 0; i <= 10; i++) {
                    v.push_back(i);
            }
    
            cout << "Before applying Sort Function\n";
            cout << "Output : ";
            for (it = v.begin(); it != v.end(); it++) {
                    cout << *it << " ";
            }
            cout << endl;
    
            struct {
                    bool operator()(int a, int b)
                    {
                            return a > b;
                    }
            } comp;
    
            // sort function using the second version
            sort(v.begin(),v.end(),comp);
    
            cout << "After applying Sort Function\n";
            cout << "Output : ";
            for (it = v.begin(); it != v.end(); it++) {
                    cout << *it << " ";
            }
            cout << endl;
    
            return 0;
    }
    

    Output of the above program

    Before applying Sort Function
    Output : 0 1 2 3 4 5 6 7 8 9 10 
    After applying Sort Function
    Output : 10 9 8 7 6 5 4 3 2 1 0 
    

    Time Complexity of sort function is

    O(N log N)
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: