c++快速排序

来源:互联网 发布:编程和数学的关系 编辑:程序博客网 时间:2024/06/05 18:51

完整代码:http://yuncode.net/code/c_503e1e448ba7745

 

核心代码:

08 int partition ( int a[], int low, int high ) 

09 { 

10     //选择第一个a[low]作为划分的临界值 

11     while ( low < high ) 

12     { 

13         while ( low < high && a[low] <= a[high] ) high--; 

14         swap ( a[low], a[high] ); 

15         while ( low < high && a[low] <= a[high] ) low++; 

16         swap ( a[low], a[high] ); 

17     } 

18     return low; 

19 } 

20   

21 void Qsort ( int a[], int low, int high ) 

22 { 

23     if ( low > high ) return; 

24     int p = partition ( a, low, high ); 

25     Qsort ( a, low, p-1 ); 

26     Qsort ( a, p+1, high ); 

27 } 

 

原创粉丝点击