快速排序的C++实现代码

    xiaoxiao2021-03-25  63

    //交换两个元素 void swap(vector<int>& A,int i,int j){ int temp = A[i]; A[i] = A[j]; A[j] = temp; } //使用双指针来实现partition int partition(vector<int>& A,int start,int end){ //支点 int pivot = (start+end)/2; //将支点交换到数组尾端 swap(A,pivot,end); int small = start-1; for(int index = start;index<end;index++){ if(A[index]<=A[end]){ small ++; if(small!=index) swap(A,small,index); } } ++ small; swap(A,small,end); return small; } void qsort(vector<int>& A,int start,int end){ if (start>=end) return; int k = partition(A,start,end); qsort(A,start,k-1); qsort(A,k+1,end); }

    转载请注明原文地址: https://ju.6miu.com/read-39213.html

    最新回复(0)