作者kumusou (一心想)
看板C_and_CPP
標題[問題] 結果不如預期(幫Debug)
時間Wed Mar 18 22:46:43 2009
以下是快速排序
但不知為啥執行後 就是沒排序
不知錯在哪裡
煩請不吝賜教 謝謝
#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
推 plover:Swap(int &a, int &b) or Swap(int *a, int*b) ... 03/18 22:48
→ tiyun:版上真多113 03/18 22:51
推 snowlike:倒數第三四行註解掉,試好第一次排序;只跑一端比對key? 03/18 23:31