Jump to content

Sort (C++): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎External links: (incorrect link)
Line 66: Line 66:
==External links==
==External links==


* [http://en.cppreference.com/w/cpp/algorithm/sort/ C++ reference for <code>std::sort</code>]
* [http://en.cppreference.com/w/cpp/algorithm/sort C++ reference for <code>std::sort</code>]
* [http://www.cplusplus.com/reference/algorithm/sort/ Another C++ reference for <code>std::sort</code>]
* [http://www.cplusplus.com/reference/algorithm/sort/ Another C++ reference for <code>std::sort</code>]



Revision as of 21:30, 1 March 2012


sort is a function in C++ Standard Library that takes two random-access iterators, the start and the end, as arguments and performs a comparison sort on the range of elements between the two iterators, front-inclusive and end-exclusive. The specific sorting algorithm is not mandated and may vary across implementations. The GNU Standard C++ library, for example, uses a hybrid sorting algorithm: introsort is performed first, to a maximum depth given by 2×log2 n, where n is the number of elements, followed by an insertion sort on the result.[1] Whatever the implementation, the complexity should be O(n log n) comparisons on the average.[2]

Usage

The sort function is included from the algorithm header of the C++ Standard Library, and carries three arguments: RandomAccessIterator first, RandomAccessIterator last, Compare comp. The third argument has default value - the "less-than" (<) operator to compare elements.

This code sample sorts a given array of integers (in ascending order) and prints it out. Pointers into the array serve as iterators.

#include <iostream>
#include <algorithm>

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 <iostream>
#include <algorithm>
#include <vector>

int main() {
  std::vector<int> 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, 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). 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.[3]

See also

External links

References

  1. ^ libstdc++ Documentation: Sorting Algorithms
  2. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §25.3.1.1 sort [lib.sort] para. 2
  3. ^ Meyers, Scott (2001). Effective STL: 50 specific ways to improve your use of the standard template library. Addison-Wesley. p. 203. ISBN 0-201-74962-9. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)