- Sort (C++)
sort is a function in
C++ Standard Template Library that takes two random-access iterators, the start and the end, as arguments and performs acomparison sort on the range of elements between the two iterators, front-inclusive and end-exclusive. The underlying algorithm is (usually)introsort , which is a combination ofquicksort andheapsort (the implementation details are not mandated). Whatever the implementation, the complexity is guaranteed to be O("n" log "n") comparisons in the worst case.sort
is specified in the algorithmheader file .Usage
The
sort
function is included from thealgorithm
header of the Standard Template Library, and carries threeargument s:iterator start, iterator end, function compare
. The third argument has default value - the "less-than" (<) operator to compare elements.This code sample sorts given array of integers (in ascending order) and print it out. Pointers into the array serve as iterators.
#include
#include int main() { int array [] = { 23, 5, -10, 0, 0, 321, 1, 2, 99, 30 }; int elements = sizeof(array) / sizeof(array [0] ); std::sort(array, array + elements); for (int i = 0; i < elements; ++i) std::cout << array [i] << ' '; } The same functionality using vector container:
#include
#include #include int main() { std::vector vec; vec.push_back(10); vec.push_back(5); vec.push_back(100); std::sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); ++i) std::cout << vec [i] << ' '; } Other types of sorting
sort is not stable: equivalent elements that are ordered one way before sorting may be ordered differently after sorting. stable_sort ensures stability of result at expense of worse performance (in some cases). Result of partial_sort is N sorted elements from M, the rest (M - N) is left in undefined order. Depending on design this may be faster than complete sort.
Some containers, among them list (part of STL), provide specialised version of sort as a member function. This is because linked lists don't have random access (and therefore can't use the regular sort function); and the specialised version also preserves the values list iterators point to.
Comparison to qsort()
sort() can be compared to the qsort() function in the C standard library (in
stdlib.h ), which performs a quicksort. sort is guaranteed to be O(N log N) in the worst case, which is better than qsort, which has O(N^2) worst case. (quick sort algorithm has O(N log N) complexity at average.) sort is templated, so it uses the correct comparison function for whatever data type you are sorting, which is already implemented for standard data types. Otherwise you can specify your own comparison function, which can be type-checked by the compiler; whereas for qsort, you must manually cast pointers to the desired type in an unsafe way. Also, qsort accesses the comparison function using a function pointer, necessitating large numbers of repeated function calls, which can take a lot of time; whereas in C++, comparison functions are usually inlined, removing the need for function calls. In practice, C++ code using sort is often many times faster at sorting simple data like integers than equivalent C code using qsort.ee also
*
Standard Template Library External links
* [http://www.sgi.com/tech/stl/sort.html SGI STL specification of sort() function]
Wikimedia Foundation. 2010.