我是一名计算机科学专业的学生(刚刚开始),我正在努力从伪代码编写快速排序的随机枢轴版本。我已经编写并测试了它,但一切都很完美......
分区部分看起来有点太复杂了,感觉漏掉了什么或者想太多了。我不明白这是否可以,或者我是否犯了一些可以避免的错误。
长话短说:它确实有效,但如何做得更好呢?
预先感谢您的所有帮助
void partition(int a[],int start,int end)
{
srand (time(NULL));
int pivotpos = 3; //start + rand() % (end-start);
int i = start; // index 1
int j = end; // index 2
int flag = 1;
int pivot = a[pivotpos]; // sets the pivot's value
while(i<j && flag) // main loop
{
flag = 0;
while (a[i]<pivot)
{
i++;
}
while (a[j]>pivot)
{
j--;
}
if(a[i]>a[j]) // swap && sets new pivot, and restores the flag
{ …Run Code Online (Sandbox Code Playgroud)