Sorting is one of the most basic functions applied to data. It means arranging the data in a particular fashion, which can be increasing or decreasing. There is a built-in function in C++ STL by the name of sort().
std::sort() is a generic function in C++ Standard Library, for doing comparison sorting.
sort(startaddress, endaddress, comparator) where: startaddress: the address of the first element of the array endaddress: the address of the last element of the array comparator: the comparison to be done with the array. This argument is optional.
Array after sorting using default sort is : 0 1 2 3 4 5 6 7 8 9
- Best Case – O(N log N)
- Average Case- O(N log N)
- Worse Case- O(N log N)
where, N = number of elements to be sorted.
Algorithms used by sort()
The algorithm used by sort() is IntroSort. Introsort being a hybrid sorting algorithm uses three sorting algorithm to minimise the running time, Quicksort, Heapsort and Insertion Sort. Simply putting, it is the best sorting algorithm around. It is a hybrid sorting algorithm, which means that it uses more than one sorting algorithms as a routine.
-313 -9 -9 1 3 23 23 32 233
Standard C library provides qsort() that can be used for sorting an array. As the name suggests, the function uses QuickSort algorithm to sort the given array
It is better to use sort() instead of qsort() because:
- sort() does not use unsafe void pointers like qsort().
- qsort() makes large number of function calls for comparison function compared to sort().
- C++ code with sort() is relatively faster than code with qsort().
Detailed article : Comparison of sort() with qsort()
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- TCS Ninja Interview Experience (CSE) 2018 (In-Details)
- ios manipulators internal() function in C++
- Count minimum number of moves to front or end to sort an array
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Check if a string can be transformed to another by sorting substrings
- Maximum Manhattan distance between a distinct pair from N coordinates
- Longest increasing subsequence which forms a subarray in the sorted representation of the array
- Lexicographic rank of a string among all its substrings
- Minimize cost to convert a given matrix to another by flipping columns and reordering rows
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Minimum distance between any special pair in the given array
- Find the interval which contains maximum number of concurrent meetings
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.