我想知道如何實現快速排序而不必創建額外的數組。然而,這個實現只在我使用1作爲關鍵點時才起作用。當我使用任何其他號碼時,程序段發生故障。我無法弄清楚哪個變量超出界限導致無限循環。對於此功能的任何批評/幫助,我將不勝感激。在不使用多個數組的情況下在C中執行Quicksort實現
void quick_sort(Item a[], int max, int pivot)
{
int i, j, p, t;
printf("%d", pivot);
if (max < 2)
{
return;
}
p = a[pivot];
printf("%d", p);
for (i = 0, j = max-1;; i++, j--)
{
while (a[i] < p)
{
i++;
}
while (p < a[j])
{
j--;
}
if (i >= j)
{
break;
}
t = a[i];
a[i] = a[j];
a[j] = t;
}
quick_sort(a, i, pivot);
quick_sort(a+i, max-i, pivot);
}
使用隨機函數從排序的每個階段的可用數字中選擇數據透視。去看看wikipedia的僞代碼 – softwarenewbie7331