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()
- TCS Ninja Interview Experience (CSE) 2018 (In-Details)
- Find the minimum number of rectangles left after inserting one into another
- Largest number not greater than N which can become prime after rearranging its digits
- Product of all Subsequences of size K except the minimum and maximum Elements
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximum absolute difference in an array
- Count the pairs of vowels in the given string
- Print 2-D co-ordinate points in ascending order followed by their frequencies
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Split the array elements into strictly increasing and decreasing sequence
- Divide array into two parts with equal sum according to the given constraints
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Shell-Metzner Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.