gpt4 book ai didi

c++ - 双轴快速排序

转载 作者:行者123 更新时间:2023-11-28 07:49:18 25 4
gpt4 key购买 nike

<分区>

我有一项任务是实现双枢轴快速排序算法。它似乎适用于具有少量数字的 vector ,但是当我尝试对具有例如 100000 的 vector 进行排序时,我遇到了段错误。有什么帮助吗?

void quicksort_dual_pivot(vector <int> &A, int L, int R)
{

if(L>=R) return;

int spivot = A[L]; //Error here.
int bpivot = A[R];


if(spivot > bpivot){

swap(A[R],A[L]);
swap(spivot,bpivot);
}

int l = L+1;

int g = R-1;



for(int k=l;k<=g;k++){

if(A[k] < spivot) {


swap(A[k],A[l]);
l++;
}

else if(A[k] > bpivot){
while(A[g] > bpivot && k < g){

g--;
}

swap(A[g],A[k]);
g--;

if(A[k] < spivot){

swap(A[k],A[l]);
l++;

}
}

}

l--;
g++;

swap(A[L],A[l]);

swap(A[R],A[g]);



quicksort_dual_pivot(A,L,l-1);

quicksort_dual_pivot(A,l+1,g-1); // And error here.

quicksort_dual_pivot(A,g+1,R);

}

谢谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com