1. 程式人生 > >C標準庫中的快速排序(quick-sort)函式 [簡單應用]

C標準庫中的快速排序(quick-sort)函式 [簡單應用]

#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
請按任意鍵繼續. . .
---------------------------------*/