-1
我想實現快速排序來根據它們的長度來排序字符串。我在做什麼是錯的,我不能爲我的生活找出什麼是不正確的。我希望另一組眼睛可以檢查我的代碼並找出我出錯的地方。如何將QuickSort集成到一個字符串向量中,我必須對字符串的長度進行排序?
void justAQuickieSort(int left, int right)
{
//left and right given by left = 0 and right = copy.begin();
if(left < right)
{
int part = partition(left, right);
justAQuickieSort(left, part - 1);
justAQuickieSort(part + 1, right);
}
}
int partition(int left, int right) {
string temp = " ";
const int mid = left + (right - left)/2;
// move the mid point value to the front.
temp = copy[mid];
copy[mid] = copy[left];
copy[left] = temp;
int i = left + 1;
int j = right;
while (i <= j) {
while(i <= j && copy[i].size() <= copy[mid].size()) {
i++;
}
while(i <= j && copy[j].size() > copy[mid].size()) {
j--;
}
if (i < j) {
cout << "here" << endl;
temp = copy[i];
copy[i] = copy[j];
copy[j] = temp;
}
}
temp = copy[i-1];
copy[i-1] = copy[left];
copy[left] = temp;
return i-1;
感謝您給予的任何幫助。
一些建議:如果你要寫你自己的排序,我建議你寫一個模板版本,根據給出的類型進行排序。一旦你這麼做了(比如用簡單的'int'類型),將進行比較的部分分解爲一個單獨的函數,該函數接受兩個參數,並根據arg1
PaulMcKenzie
@PaulMcKenzie如果我確實這樣做了,那麼我會在哪裏把它放在我的快速排序中? – terrabl