算法导论(implementation of quick sort)

来源:互联网 发布:历年进出口数据查询 编辑:程序博客网 时间:2024/06/05 15:17
#include<iostream>#include<vector>#include<string>#include<set>#include<map>#include<unordered_set>#include<unordered_map>#include<algorithm>#include<xfunctional>using namespace std;int Part(vector<int>& data,int start,int end){int pivot = data[start];while (start < end){while (start < end&&data[end] >= pivot) end--;data[start] = data[end];while (start < end&&data[start] <= pivot) start++;data[end] = data[start];}data[start] = pivot;return start;}void QuickSort(vector<int>& data,int start,int end){if (start < end){int index = Part(data,start,end);QuickSort(data,start,index-1);QuickSort(data,index+1,end);}}int main(){int n;cout << "Input the amount of the data:";cin >> n;vector<int> data(n);for (int i = 0; i < n; i++) cin >> data[i];int end = data.size()-1;QuickSort(data,0,end);cout << "After sort:";for (int i = 0; i < data.size(); i++) cout << data[i] << " ";cout << endl;system("pause");return 0;}

0 0