快速排序QuickSort

来源:互联网 发布:郑州直销软件靠谱吗 编辑:程序博客网 时间:2024/06/05 23:46

快速排序是基于划分的排序。

具体过程就不表了。

划分算法

int Partition(int* pData, int begin, int end){if (NULL == pData || begin > end)return -1;int pivot = begin;int front = begin;int back = end;while (front < back){// 先从后面向先前找小于基准值的项while (front < back && pData[back] >= pData[pivot])--back;// 再从前往后面找大于基准值的项while (front < back && pData[front] <= pData[pivot])++front;// 如果都找到if (front < back)Swap(pData[front], pData[back]);}// 注意:交换的是pivot和backif (pivot != back)Swap(pData[pivot], pData[back]);return back;}

快速排序

void QuickSort(int* pData, int begin, int end){if (NULL == pData || begin >= end)return;int pivot = Partition(pData, begin, end);QuickSort(pData, begin, pivot - 1);QuickSort(pData, pivot + 1, end);}



0 0