快速排序

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <exception>
 4 #include <stack>
 5 using namespace std;
 6 
 7 void Swap(int *lhs,int *rhs)
 8 {
 9     int temp = *lhs;
10     *lhs = *rhs;
11     *rhs = temp;
12 }
13 
14 int Partition(int* Array,int begin,int end)
15 {
16     int ran = begin+ rand() % (end-begin+1);//生成一个从begin到end的随机数
17     Swap(&Array[ran],&Array[end]);
18     int key = Array[end];
19     int i = begin-1;
20     for(int j = begin;j != end;++j)
21     {
22         if(Array[j]<key)
23         {
24             ++i;
25             Swap(&Array[i],&Array[j]);
26         }
27     }
28     Swap(&Array[i+1],&Array[end]);
29     return i+1;
30 }
31 void QuickSort(int *arrlist,int beg,int end)
32 {
33     if(beg < end)
34     {
35         int index = Partition(arrlist,beg,end);
36         QuickSort(arrlist,beg,index-1);
37         QuickSort(arrlist,index+1,end);
38     }
39 }
40 int _tmain(int argc, _TCHAR* argv[])
41 { 
42     int arr[]={1,2,3,10,6,7,8,5,11,4,9,14,0};
43     int LENGTH = sizeof(arr)/sizeof(int);
44     QuickSort(arr,0,LENGTH-1);
45     //快速排序之后的结果
46     for(int i = 0;i != LENGTH;++i)
47         cout<<arr[i]<<" ";
48     return 0 ;
49 }
原文地址:https://www.cnblogs.com/crazycodehzp/p/3557556.html