對不起,我知道有關於quicksort的一堆問題,但有一個錯誤,我不能找到。快速排序不正確
public static void QuickSort(int[] sort)
{
QuickSort(sort, 0, sort.Length - 1);
}
private static void QuickSort(int[]sort, int first, int last)
{
int i = first;
int j = last;
int pivot = (i + j)/2;
int temp;
while(i <= j)
{
while(sort[i] < sort[pivot]){i++;}
while(sort[j] > sort[pivot]){j--;}
if(i <= j)
{
temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
i++;
j--;
}
}
if(first < j)QuickSort(sort, first, j);
if(last > i)QuickSort(sort, i, last);
}
這似乎不錯,但排序這個數組
int[] sortMe = {2,5,6,4,8,9,6,3,21,2,5,4,8,9,6,5,46,6,3};
這樣的:
2,2,3,4,3,4,5,5,6,6,5,6,6,8,8,9,9,21,46
,而不是明顯的正確的順序。
嘗試對每個非常小的數組進行排序,直到找到一個不起作用的地方,然後通過調試器運行它,並將其與僞代碼/它應該做什麼比較 – Patashu 2013-04-29 05:49:02
使用此鏈接http://stackoverflow.com/questions/2095153 /快速排序 - 不正確排序 – Rahul 2013-04-29 05:50:48
不幸的是,在當前狀態下,這個問題不太可能對SO訪問者有用。如果你想自己編寫代碼 - 那麼做一下,調試(更好地添加基本測試)並使其工作。否則,只需使用現有的排序方法您可以通過顯示已經驗證過的測試用例(以及添加的單元測試)來改善您的問題。 – 2013-04-29 05:57:07