quick sort
// QuickSort.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" int partition( int*p, int l, int r ) { int val,ml, mr, tmp; val = p[l]; ml=l+1; mr=r; /// while(1) { for( ; ml<mr; ml++ ) { if( p[ml] > val ) break; } for( ; mr>ml; mr-- ) { if( p[mr] <= val ) break; } if( mr<=ml ) break; tmp = p[ml]; p[ml]=p[mr]; p[mr]=tmp; // ++ml; --mr; }//!while if( p[mr] > val ) --mr; tmp = p[mr]; p[mr] = val; p[l] = tmp; return mr; } void quicksort( int*p, int l, int r ) { if( l<r ) { int m=partition( p, l, r ); quicksort( p, l, m-1 ); quicksort( p, m+1, r ); } } int _tmain(int argc, _TCHAR* argv[]) { int test[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; int test1[5] = { 1, 2, 3, 4, 5 }; int test2[5] = { 9, 3, 1, 6, 5 }; quicksort( test2, 0, 4 ); return 0; }