What is the std::sort() function in C++ STL?

We have examined qsort() in C. C++ STL gives a comparative capacity sort that sorts a vector or exhibit (things with arbitrary access). std::sort() is an inherent capacity in C++’s Standard Template Library. The capacity takes in a start iterator, a consummation iterator, and (of course) sorts the iterable in rising request. The capacity can also​ be utilized for custom arranging by passing in a comparator work that profits a boolean.

It for the most part takes two boundaries, the first being the mark of the cluster/vector from where the arranging needs to start and the subsequent boundary being the length up to which we need the exhibit/vector to get arranged. The third boundary is discretionary and can be utilized in cases, for example, to sort the components lexicographically.

As a matter of course, the sort() work sorts the components in rising request.

// C++ program to demonstrate default behaviour of
// sort() in STL.
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    /*Here we take two parameters, the beginning of the
    array and the length n upto which we want the array to
    be sorted*/
    sort(arr, arr + n);
    cout << "\nArray after sorting using "
            "default sort is : \n";
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    return 0;
}

Output:

Array after sorting using default sort is : 
0 1 2 3 4 5 6 7 8 9

How to sort in plummeting requests?

sort() takes a third boundary that is utilized to indicate the request where components are to be arranged. We can pass the “more noteworthy()” capacity to sort in slipping requests. This capacity does an examination that puts more noteworthy components previously.

// C++ program to demonstrate descending order sort using
// greater<>().
#include <bits/stdc++.h>
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, greater<int>());
cout << "Array after sorting : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}

Output:

Array after sorting : 
9 8 7 6 5 4 3 2 1 0

How to sort in a specific request?

We can likewise compose our own comparator capacity and pass it as a third boundary. This “comparator” work returns a worth; convertible to bool, which fundamentally lets us know whether the passed “first” contention ought to be set before the passed “second” contention or not.

For eg: In the code underneath, assume stretches {6,8} and {1,9} are passed as contentions in the “compareInterval” function(comparator work). Presently as i1.first (=6) > i2.first (=1), so our capacity returns “bogus”, which lets us know that “first” contention ought not be put previously “second” contention thus arranging will be done all together like {1,9} first and afterward {6,8} as next.

// A C++ program to demonstrate
// STL sort() using
// our own comparator
#include <bits/stdc++.h>
using namespace std;
// An interval has a start
// time and end time
struct Interval {
    int start, end;
};
// Compares two intervals
// according to starting times.
bool compareInterval(Interval i1, Interval i2)
{
    return (i1.start < i2.start);
}
int main()
{
    Interval arr[]
        = { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
    int n = sizeof(arr) / sizeof(arr[0]);
    // sort the intervals in increasing order of
    // start time
    sort(arr, arr + n, compareInterval);
    cout << "Intervals sorted by start time : \n";
    for (int i = 0; i < n; i++)
        cout << "[" << arr[i].start << "," << arr[i].end
             << "] ";
    return 0;
}

Output: 

Intervals sorted by start time : 
[1,9] [2,4] [4,7] [6,8]

The time intricacy of std::sort() is:

  • Best Case – O(N log N)
  • Normal Case – O(N log N)
  • Thinking pessimistically – O(N log N)

Space Complexity – It might utilize O( log N) assistant space.

This article is contributed by Shubham Agrawal. Kindly record remark assuming that you find anything wrong, or you need to share more data about the theme examined previously

Also ReadHow to set checkbox size in HTML/CSS?

Leave a Reply

Your email address will not be published. Required fields are marked *