Quick Sort

来源:互联网 发布:淘宝上的装修公司 编辑:程序博客网 时间:2024/04/29 09:26



#include <vector>using namespace std;/* * choose the first element as pivot */int partition(vector<int> &v, int start, int end) {int pivot = v[start];int left = start + 1;int right = end;while (true) {while (left < right && v[left] < pivot) left++;while (right > left && v[right] >= pivot) right++;if (left == right)  break;int temp = v[left];v[left] = v[right];v[right] = temp;}if(v[left] >= pivot) return left;int tmp = v[left];v[left] = pivot;v[start] = tmp;return left;}void quickSort(vector<int> &v, int start, int end) {if(start >= end) return;int boundary = partition(v, start, end);quickSort(v, start, boundary -1);quickSort(v, boundary + 1, end);}