[問題] 結果不如預期(幫Debug)

看板C_and_CPP (C/C++)作者 (一心想)時間16年前 (2009/03/18 22:46), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串1/2 (看更多)
以下是快速排序 但不知為啥執行後 就是沒排序 不知錯在哪裡 煩請不吝賜教 謝謝 #include <iostream> using namespace std; void QuickSort(int *, int , int); void Swap(int , int); int main(){ cout << "QuickSort!!\nEnter the unsorted lists's size:"; int size = 0; cin >> size; int *a = new int [size]; cout << "The list:"; for(int i = 0; i < size; i++){ cin >> a[i]; } QuickSort(a, 0, size-1); cout << "The output:"; for(int i = 0; i < size; i++) cout << a[i] << " "; cout << endl; delete []a; system("pause"); return 0; } void Swap(int a, int b){ int temp = a; a = b; b = temp; } void QuickSort(int *a, int left, int right){ if(left < right){ int key = a[right]; int i = left - 1; for(int j = left; j < right; j++){ if(a[j] <= key){ i++; Swap(a[i], a[j]); } } Swap(a[i+1], a[right]); i++; QuickSort(a, left, i-1); QuickSort(a, i+1, right); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.91.18

03/18 22:48, , 1F
Swap(int &a, int &b) or Swap(int *a, int*b) ...
03/18 22:48, 1F

03/18 22:51, , 2F
版上真多113
03/18 22:51, 2F

03/18 23:31, , 3F
倒數第三四行註解掉,試好第一次排序;只跑一端比對key?
03/18 23:31, 3F
文章代碼(AID): #19mGbKXU (C_and_CPP)
文章代碼(AID): #19mGbKXU (C_and_CPP)