#include <iostream>
#include <cstdlib>
using namespace std;
const size_t INDEX_ZERO = 0;
int com(const void *a,const void *b)
{
return *(int*)a - *(int*)b;
}
void display( const int* array, size_t size )
{
if (NULL == array)
{
throw;
}
else
{
for(size_t i=INDEX_ZERO; i<size; ++i)
{
cout << array[i] << " ";
}
cout << endl;
}
}
int main( void )
{
int array[] = {9, 1, 7, 3, 4, 8, 2, 6, 0, 5};
const size_t SIZE = sizeof (array) / sizeof (*array);
display( array, SIZE );
qsort(array, SIZE, sizeof (*array), com);//C标准库中的快速排序(quick-sort)函数
display( array, SIZE );
system( "PAUSE" );
return EXIT_SUCCESS;
}
/*-------------------
9 1 7 3 4 8 2 6 0 5
0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
---------------------------------*/C标准库中的快速排序(quick-sort)函数 [简单应用]
最新推荐文章于 2024-01-10 18:48:52 发布
本文展示了一个使用C语言进行数组排序并显示排序前后结果的例子。通过调用C标准库中的快速排序函数qsort实现对整型数组的排序,并自定义了比较函数com以及显示数组元素的函数display。
697

被折叠的 条评论
为什么被折叠?



