📄 sort.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>sort</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Algorithms</a> > <a href="sort.html">sort</a> </div> <div class="name-format"> sort </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <algorithm> void sort( iterator start, iterator end ); void sort( iterator start, iterator end, StrictWeakOrdering cmp );</pre> <p>The sort() algorithm sorts the elements in the range [<em>start</em>,<em>end</em>) into ascending order. If two elements are equal, there is no guarantee what order they will be in.</p> <p>If the strict weak ordering function object <em>cmp</em> is given, then it will be used to compare two objects instead of the < operator.</p> <p>The algorithm behind sort() is the <em>introsort</em> algorithm. sort() runs in O(N log(N)) time (average and worst case) which is faster than polynomial time but slower than <a href= "../complexity.html">linear time</a>.</p> <div class="related-examples-format"> Example code: </div> <div class="related-examples"> <p>For example, the following code sorts a vector of integers into ascending order:</p> <pre class="example-code"> vector<int> v; v.push_back( 23 ); v.push_back( -1 ); v.push_back( 9999 ); v.push_back( 0 ); v.push_back( 4 ); cout << "Before sorting: "; for( unsigned int i = 0; i < v.size(); i++ ) { cout << v[i] << " "; } cout << endl; sort( v.begin(), v.end() ); cout << "After sorting: "; for( unsigned int i = 0; i < v.size(); i++ ) { cout << v[i] << " "; } cout << endl; </pre> <p>When run, the above code displays this output:</p> <pre class="example-code"> Before sorting: 23 -1 9999 0 4 After sorting: -1 0 4 23 9999 </pre> <p>Alternatively, the following code uses the sort() function to sort a normal array of integers, and displays the same output as the previous example:</p> <pre class="example-code"> int array[] = { 23, -1, 9999, 0, 4 }; unsigned int array_size = 5; cout << "Before sorting: "; for( unsigned int i = 0; i < array_size; i++ ) { cout << array[i] << " "; } cout << endl; sort( array, array + array_size ); cout << "After sorting: "; for( unsigned int i = 0; i < array_size; i++ ) { cout << array[i] << " "; } cout << endl; </pre> <p>This next example shows how to use sort() with a user-specified comparison function. The function <strong>cmp</strong> is defined to do the opposite of the < operator. When sort() is called with <strong>cmp</strong> used as the comparison function, the result is a list sorted in descending, rather than ascending, order:</p> <pre class="example-code"> bool cmp( int a, int b ) { return a > b; } ... vector<int> v; for( int i = 0; i < 10; i++ ) { v.push_back(i); } cout << "Before: "; for( int i = 0; i < 10; i++ ) { cout << v[i] << " "; } cout << endl; sort( v.begin(), v.end(), cmp ); cout << "After: "; for( int i = 0; i < 10; i++ ) { cout << v[i] << " "; } cout << endl; </pre> </div> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="binary_search.html">binary_search</a><br> <a href="is_sorted.html">is_sorted</a><br> <a href="merge.html">merge</a><br> <a href="partial_sort.html">partial_sort</a><br> <a href="partial_sort_copy.html">partial_sort_copy</a><br> <a href="stable_sort.html">stable_sort</a><br> (Other Standard C Functions) <a href= "../stdother/qsort.html">qsort</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -