其实研究算法的时候,最好的做法就是拿出纸和笔,然后自己一行一行代码的走下去。
这样学习的速度我觉得是最快的。
快排的基本思想是:通过一次排序将整个序列分成两部分,其中一部分的记录均比关
键值小,另一部均比关键值大。然后在分别对两个小部分进行同样的思想处理。
代码:
1 #include <iostream> 2 3 using namespace std; 4 5 void Quicksort(int *iArr, int low, int high); 6 int Partition(int *iArr, int low, int high); 7 //int Partition2(int *iArr, int low, int high); 8 void Swap(int &i, int &j) 9 { 10 int iTemp = i; 11 i = j; 12 j = iTemp; 13 } 14 15 void Display(int iArr[], int iLen) 16 { 17 std::cout << "iArr = "; 18 for(int i = 0; i < iLen; i++) 19 { 20 std::cout << " " << iArr[i]; 21 } 22 std::cout << std::endl; 23 } 24 25 int main(int argc, char **argv) 26 { 27 int low = 0; 28 int high = 7; 29 int iArr[8] = {2, 8, 7, 1, 3, 5, 6, 4}; 30 31 Display(iArr, 8); 32 Quicksort(iArr, low, high); 33 Display(iArr, 8); 34 return 0; 35 } 36 37 void Quicksort(int *iArr, int low, int high) 38 { 39 if(low < high) 40 { 41 int iPivotLoc = Partition(iArr, low, high); 42 Quicksort(iArr, low, iPivotLoc - 1); 43 Quicksort(iArr, iPivotLoc + 1, high); 44 } 45 } 46 47 int Partition(int *iArr, int low, int high) 48 { 49 while(low < high) 50 { 51 int iPivot = iArr[low]; 52 53 while(low < high && iArr[high] >= iPivot) 54 { 55 high--; 56 } 57 58 if(low != high) 59 { 60 Swap(iArr[low], iArr[high]); 61 } 62 63 while(low < high && iArr[low] <= iPivot) 64 { 65 low++; 66 } 67 68 if(low != high) 69 { 70 Swap(iArr[low], iArr[high]); 71 } 72 } 73 74 return low; 75 }
整个算法的执行过程如下:(建议按照这个数据,然后用纸和笔手动的走一遍代码。)
iArr = 2 8 7 1 3 5 6 4
low = 0 high = 7 iPivot = 2
low = 0 high = 6 iPivot = 2
low = 0 high = 5 iPivot = 2
low = 0 high = 4 iPivot = 2
low = 0 high = 3 iPivot = 2
low = 0 high = 3 iPivot = 2 // 53行为false了
iArr = 1 8 7 2 3 5 6 4
low = 1 high = 3 iPivot = 2
low = 1 high = 3 iPivot = 2 // 63行为false了
iArr = 1 2 7 8 3 5 6 4
low = 1 high = 3 iPivot = 2
low = 1 high = 2 iPivot = 2
low = 1 high = 1 iPivot = 2
low = 1 high = 1 iPivot = 2 // 53行为false了
iArr = 1 2 7 8 3 5 6 4
low = 1 high = 1 iPivot = 2 // 63行为false了
iArr = 1 2 7 8 3 5 6 4
***************************************************** // 整个Partition函数执行完了
iArr = 1 2 7 8 3 5 6 4
low = 2 high = 7 iPivot = 7
low = 2 high = 7 iPivot = 7
iArr = 1 2 4 8 3 5 6 7
low = 3 high = 7 iPivot = 7
low = 3 high = 7 iPivot = 7
iArr = 1 2 4 7 3 5 6 8
low = 3 high = 7 iPivot = 7
low = 3 high = 6 iPivot = 7
low = 3 high = 6 iPivot = 7
iArr = 1 2 4 6 3 5 7 8
low = 4 high = 6 iPivot = 7
low = 5 high = 6 iPivot = 7
low = 6 high = 6 iPivot = 7
low = 6 high = 6 iPivot = 7
iArr = 1 2 4 6 3 5 7 8
*****************************************************
iArr = 1 2 4 6 3 5 7 8
low = 2 high = 5 iPivot = 4
low = 2 high = 4 iPivot = 4
low = 2 high = 4 iPivot = 4
iArr = 1 2 3 6 4 5 7 8
low = 3 high = 4 iPivot = 4
low = 3 high = 4 iPivot = 4
iArr = 1 2 3 4 6 5 7 8
low = 3 high = 4 iPivot = 4
low = 3 high = 3 iPivot = 4
low = 3 high = 3 iPivot = 4
iArr = 1 2 3 4 6 5 7 8
low = 3 high = 3 iPivot = 4
iArr = 1 2 3 4 6 5 7 8
*****************************************************
iArr = 1 2 3 4 6 5 7 8
low = 4 high = 5 iPivot = 6
low = 4 high = 5 iPivot = 6
iArr = 1 2 3 4 5 6 7 8
low = 5 high = 5 iPivot = 6
low = 5 high = 5 iPivot = 6
iArr = 1 2 3 4 5 6 7 8
*****************************************************
最终结果:
iArr = 1 2 3 4 5 6 7 8
好像还以一种更高效一点的写法,后续更新。