gpt4 book ai didi

c++ - 如何在没有库的情况下对 double 组进行有效排序?

转载 作者:行者123 更新时间:2023-11-30 21:26:27 25 4
gpt4 key购买 nike

我正在寻找一种有效的方法来对 double 组进行排序。我知道冒泡排序和选择排序,它们似乎都不够快。我读到了快速排序,但我不明白它是如何工作的。有很多示例源代码,但它们的注释都很差。有人可以给我解释一下吗?

最佳答案

我在了解 qsort 的工作原理后写了这篇文章。我确实认为 qsort 不是那么容易理解。它可能需要一些优化,并且与原始的 qsort 相比可能没有什么地方,但它就是这样。感谢那些试图为此提供帮助的人。

/*recursive sorting, throws smaller values to left,
bigger to right side, than recursively sorts the two sides.*/
void sort(double szam[], int eleje, int vege){
if (vege > eleje + 1){ //if I have at least two numbers
double kuszob = szam[eleje]; //compare values to this.
int l = eleje + 1; //biggest index that is on the left.
int r = vege; //smallest index that is on the right side.
while (l < r){ //if I haven't processed everything.
if (szam[l] <= kuszob) l++; //good, this remains on the left.
else
swap(&szam[l], &szam[--r]); //swap it with the farthest value we haven't checked.
}
swap(&szam[--l], &szam[eleje]); //make sure we don't compare to this again, that could cause STACK OVERFLOW
sort(szam, eleje, l); //sort left side
sort(szam, r, vege); //sort right side
}
return; //if I have 1 number break recursion.
}

关于c++ - 如何在没有库的情况下对 double 组进行有效排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15769337/

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