2016-03-21 8 views
0

Ich versuche, einen Vektor von Geburtstagen (mit meiner Implementierung von Quicksort) zu sortieren, und ändern Sie auch die Reihenfolge der zwei Vektoren mit Namen und Geburtsdaten basierend darauf, wie sie sich ändern. Ich habe eine Online-Quelle über die Implementierung von Quicksort verfolgt, bin mir aber nicht ganz sicher, warum das nicht funktionieren wird. Hier ist mein Code:Quicksort-Vorlage funktioniert nicht, und mein Programm reagiert nicht

template <class T> 
void sortBDay(vector<T> &birthday, vector<string> &name, vector<T> &birthdate, int startPos, int size) { // This template sorts all data by their birthday 
    if (startPos < size - 1) { // if the first value is less than the last value 
     T pivotVal = birthday[startPos]; // the pivot value is the vector's first value 
     int pivotPos = startPos; // the pivot position is the vector's starting position 
     for (int pos = startPos + 1; pos <= size; pos++) { // repeat for all values of vector 
      if (birthday[pos] < pivotVal) { // if the current position is less than the starting position 
       swap(birthday[pivotPos + 1], birthday[pos]); 
       swap(birthday[pivotPos], birthday[pivotPos + 1]); // switch the positions 

       swap(name[pivotPos + 1], name[pos]); // and their names 
       swap(name[pivotPos], name[pivotPos + 1]); 

       swap(birthdate[pivotPos + 1], birthdate[pos]); // and their birthdates 
       swap(birthdate[pivotPos], birthdate[pivotPos + 1]); 
       pivotPos++; // then go onto the next one 
      } 
      sortBDay(birthday, name, birthdate, startPos, size - 1); // do the same for upper and lower pivots 
      sortBDay(birthday, name, birthdate, startPos, size + 1); // recursion 
     } 
    } 
} 

Ich weiß nicht, was ist falsch mit dieser Implementierung. Jede Hilfe wäre willkommen! Danke im Voraus.

+1

'pos <= Größe' - das sieht gar nicht gut aus. Dies wäre wesentlich sauberer, wenn alle diese Daten in einem einzelnen Vektor von Objekten statt in drei verschiedenen Vektoren wären. Und fwiw, ['std :: partition'] (http://en.cppreference.com/w/cpp/algorithm/partition), macht den Quicksort-Algorithmus kurz und eliminiert die hier gemachten Fehler. Das verknüpfte Dokument hat sogar ein Verwendungsbeispiel, das genau das tut; implementiert Quicksort. – WhozCraig

+0

Muss ich die separate Partitionsfunktion hinzufügen, oder kann ich alles zusammenhalten? – Cool

+0

Sie können alles zusammenhalten, wenn Sie sicherlich Ihren eigenen Partitionsalgorithmus verwenden möchten. – WhozCraig

Antwort

2

Sie setzen eine Rekursion in eine Schleife, so funktioniert die schnelle Sortierung nicht. Und die Anfangs- und Endposition, die an die Rekursionsfunktion übergeben wurden, waren nicht korrekt.

Hier ist die Lösung. Ich habe den Parameter size in end geändert, weil sich die Variable in Ihrem Code so verhält.

template <class T> 
void sortBDay(vector<T> &birthday, vector<string> &name, vector<T> &birthdate, int startPos, int end) { // This template sorts all data by their birthday 
    if (startPos < end - 1) { // if the first value is less than the last value 
     T pivotVal = birthday[startPos]; // the pivot value is the vector's first value 
     int pivotPos = startPos; // the pivot position is the vector's starting position 
     for (int pos = startPos + 1; pos < end; pos++) { // repeat for all values of vector 
      if (birthday[pos] < pivotVal) { // if the current position is less than the starting position 
       swap(birthday[pivotPos + 1], birthday[pos]); 
       swap(birthday[pivotPos], birthday[pivotPos + 1]); // switch the positions 

       swap(name[pivotPos + 1], name[pos]); // and their names 
       swap(name[pivotPos], name[pivotPos + 1]); 

       swap(birthdate[pivotPos + 1], birthdate[pos]); // and their birthdates 
       swap(birthdate[pivotPos], birthdate[pivotPos + 1]); 
       pivotPos++; // then go onto the next one 
      } 
     } 

     sortBDay(birthday, name, birthdate, startPos, pivotPos); // do the same for upper and lower pivots 
     sortBDay(birthday, name, birthdate, pivotPos + 1, end); // recursion 
    } 
} 
+0

Danke! Ich werde sicher sein, das zu testen, sobald ich zu Hause bin. Entschuldigung für die späte Antwort. – Cool

+0

Es funktioniert! Vielen Dank dafür! – Cool

+1

Gern geschehen. Ich habe gerade gelernt, dass dies eine Aufgabe ist. Ich hoffe, dass Sie diesen Quick-Sort-Code sorgfältig lesen und verstehen werden. – HenryLee

Verwandte Themen