PDF document-41964FCD5818-1
PDF document-41964FCD5818-1
We have discussed qsort() in C. C++ STL provides a similar function sort that
sorts a vector or array (items with random access).
It generally takes two parameters, the first one being the point of the
array/vector from where the sorting needs to begin and the second parameter
being the length up to which we want the array/vector to get sorted. The third
parameter is optional and can be used in cases such as if we want to sort the
elements lexicographically.
CPP
:
// 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]);
return 0;
}
Output :
CPP
:
// 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]);
return 0;
}
Output:
int main()
{
Interval arr[]
= { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output: ▲
:
StartIntervals sorted by start time :
Your[1,9] [2,4] [4,7] [6,8]
Coding Login Register
Journey
The time complexity of std::sort() is:
Now!
1. Best Case – O(N log N)
2. Average Case – O(N log N)
3. Worst Case – O(N log N)
?list=PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB
Previous Next
R ECO M M E N D E D A RT I C L E S Page : 1 2 3 4 5 6
GeeksforGeeks
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
:
5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305
Company
About Us
Careers
Privacy Policy
Contact Us
Copyright Policy
Learn
Algorithms
Data Structures
Machine learning
CS Subjects
Video Tutorials
News
Technology
Business
Finance
Lifestyle
:
Languages
Python
Java
CPP
Golang
C#
Web Development
Web Tutorials
HTML
CSS
JavaScript
Bootstrap
Contribute
Write an Article
Internships
Video Internship