⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsort.html

📁 从www.CppReference.com打包的C++参考手册
💻 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>qsort</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> &gt; <a href=    "index.html">Other Standard C Functions</a> &gt; <a href=    "qsort.html">qsort</a>  </div>  <div class="name-format">    qsort  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;stdlib.h&gt;  void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );</pre>  <p>The qsort() function sorts <em>buf</em> (which contains  <em>num</em> items, each of size <em>size</em>) using <a href=  "http://en.wikipedia.org/wiki/Quicksort">Quicksort</a>. The  <em>compare</em> function is used to compare the items in  <em>buf</em>. <em>compare</em> should return negative if the first  argument is less than the second, zero if they are equal, and  positive if the first argument is greater than the second. qsort()  sorts <em>buf</em> in ascending order.</p>  <div class="related-examples-format">    Example code:  </div>  <div class="related-examples">    <p>For example, the following bit of code uses qsort() to sort an    array of integers:</p>    <pre class="example-code"> int compare_ints( const void* a, const void* b ) {   int* arg1 = (int*) a;   int* arg2 = (int*) b;   if( *arg1 &lt; *arg2 ) return -1;   else if( *arg1 == *arg2 ) return 0;   else return 1; }               int array[] = { -2, 99, 0, -743, 2, 3, 4 }; int array_size = 7;             ...             printf( &quot;Before sorting: &quot; ); for( int i = 0; i &lt; array_size; i++ ) {   printf( &quot;%d &quot;, array[i] ); } printf( &quot;\n&quot; );               qsort( array, array_size, sizeof(int), compare_ints );          printf( &quot;After sorting: &quot; ); for( int i = 0; i &lt; array_size; i++ ) {   printf( &quot;%d &quot;, array[i] ); } printf( &quot;\n&quot; );              </pre>    <p>When run, this code displays the following output:</p>    <pre class="example-code"> Before sorting: -2 99 0 -743 2 3 4 After sorting: -743 -2 0 2 3 4 99              </pre>  </div>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="bsearch.html">bsearch</a><br>    (C++ Algorithms) <a href="../cppalgorithm/sort.html">sort</a>  </div>  </div>  </td>    </tr>  </table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -