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

看板C_and_CPP (C/C++)作者 (tedium of chores)時間16年前 (2009/03/18 23:53), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
call by value call by address ※ 引述《kumusou (一心想)》之銘言: : 以下是快速排序 : 但不知為啥執行後 就是沒排序 : 不知錯在哪裡 : 煩請不吝賜教 謝謝 : #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); : } : } -- 正是在現實生活中拼搏,夢想才有意義 只是把自己投射到別人的夢想裡的話 跟死又有什麼兩樣 『真是一個現實主義者啊』 如果你把逃避現實叫做浪漫的話..... Kusanagi Motoko草薙素子 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.176.163
文章代碼(AID): #19mHZaTA (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19mHZaTA (C_and_CPP)