quick sort(快速排序)代码

#include<iostream>
using namespace std;
#include<vector>


void quicksort(int *a,int low,int high){

int mid;

mid = findmid(a,low,high); //找到第一个停止的点。
quicksort(a,low,mid-1); //以停止点为中心,继续便利左边。
quicksort(a,mid+1,high);  //继续遍历右边。



}

int findmid(int *a,int low,int high){
int i,j = low, high;
int mid = a[low];
while(i<j){
    while(i<j&&mid<a[j]){
    //当左边的基准值小于右边的每一个变量j--就往左继续移动,直到找到小于基准值的点。
    j--;

}
if(i<j){
    //进行交换。
    swap(a[j],a[i+1];)

}

while(i<j&&a[i]<=mid){


    i++;
}
if(i<j){
    swap(a[i],a[j--];)
}
}
return i;




}
void swap(int &a,int &b){

int temp;
temp = a;
a = b;
b = temp;

}
int main(){

int a[2,1,3,4,2];

quicksort(a,0,4);



return 0;}

猜你喜欢

转载自blog.csdn.net/qq_40962125/article/details/131515536