c语言快速排序库函数
2008-12-05 13:30 Iron 阅读(657) 评论(0) 编辑 收藏 举报今天看书,突然发现c语言竟然有快速排序函数,写上来,做个笔记,呵呵!
#include <iostream>
#include <cstdlib>
using namespace std;
int compare( const void * p1, const void * p2 )
{
if( *(int*)p1 > *(int*)p2 )
return 1;
else if( *(int*)p1 < *(int*)p2 )
return -1;
else
return 0;
}
int main()
{
int a[5] = { 1 , 5 , 2 , 8 , 6 };
qsort( (void*)a, 5, sizeof(int), compare );
for( int i = 0; i < 5; i++ )
cout << a[i] << endl;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int compare( const void * p1, const void * p2 )
{
if( *(int*)p1 > *(int*)p2 )
return 1;
else if( *(int*)p1 < *(int*)p2 )
return -1;
else
return 0;
}
int main()
{
int a[5] = { 1 , 5 , 2 , 8 , 6 };
qsort( (void*)a, 5, sizeof(int), compare );
for( int i = 0; i < 5; i++ )
cout << a[i] << endl;
}