AgPro

导航

qsort-库函数

#include <iostream>
#include <cstdlib>
using namespace std;

int compare_ints( const void* a, const void* b ) 
{   
	int* arg1 = (int*) a;   
	int* arg2 = (int*) b;   
	if( *arg1 < *arg2 ) 
		return -1;   
	else if( *arg1 == *arg2 ) 
		return 0;   
	else 
		return 1; 
} 

int main()
{
	int n = 5;
	int x[] = {5,1,7,4,2};

	qsort(x,n,sizeof(int),compare_ints);

	for ( int i=0; i<=n-1; i++ )
		cout << x[i] << " ";
	cout << endl;

	return 0; 
}

posted on 2010-06-11 14:14  AgPro  阅读(173)  评论(0编辑  收藏  举报