-1

我试图在这样的数组上实现快速排序int64_t

void quicksort (int64_t *array,size_t size) { 
    int64_t *split;
    size_t i=0;
    size_t j=size-1;
    if (size>1) {
    split=({
      int64_t p=array[0];
      do {
           for (;array[i]<p;i++);
           for (;array[j]>p;j--);
           swap(array[i],array[j]);
      } while (i<j);
      swap(array[i],array[j]);
      swap(array[j],array[size]);
      &(array[j]);
      })-1;
    quicksort(array,j-1);
    quicksort(split+1,size-j);
    }
    return; 
}

这很好,但是,它在第​​一次分区后立即进入无限递归或无限循环。我该如何解决这个问题?

4

2 回答 2

0

您的分区代码看起来很奇怪,并且至少存在一些问题: for (; a[i] < p; i++) 不能保证终止。

然后如果 j 是枢轴元素的最终位置,则左数组应按 quicksort(array, j) 而不是 j-1 排序。

于 2015-05-24T16:40:00.690 回答
0

I couldn't get your code to compile (my compiler is old). But shouldn't:

size_t i=0; 

be instead initialized as 1 because you have

int64_t p=array[0];  // partition element?

and

for (;array[i]<p;i++);

That is probably not the only problem.

于 2015-05-24T16:47:27.253 回答