我正在尝试使用随机枢轴实现快速排序
void QuickSort(int *arr,int left,int right){
if(right-left+1 > 2){
int i = 0,storeIndex = left;
int pivot = left + (int)(rand() % (right-left+1));
swap(&arr[pivot],&arr[right]);
for(i = left; i < right; i++){
if(arr[i] < arr[right]){
swap(&arr[i],&arr[storeIndex]);
storeIndex++;
}
}
swap(&arr[storeIndex],&arr[right]);
QuickSort(arr,left,storeIndex-1);
QuickSort(arr,storeIndex+1,right);
}
}
但这会给出排序不正确的输出,例如
未排序: [81,87,47,59,81,18,25,40,56,0]
排序: [0,18,25,40,56,47,59,81,87,81]
我的算法有问题,因为 python 中类似的东西运行良好