Ich habe ein Problem mit meinem Programm. Mein Algorithmus möchte nicht zusammenführen, wenn die Elementnummer anders ist als 2^n. Es führt Operationen vor merging.For Instanz zur Eingabe 13 5 10 8 6 22 11 3 12 20 7 9 14 17 19 1 2 18
1,3,5,6,7,8,9,10,11,12,13,14,17,19,20,22,2,18,
es zurückkehrt, funktioniert es für PO 1 bis 16 dann von 17 zu 18Sortieren zusammenführen - nur sortieren 2^n Anzahl der Elemente
Functions
void mergeSort(int arr[], int arrSort[], int first, int mid, int last) {
int from = first;
int to = mid;
for (int i = first; i<last; i++) {
if ((arr[from] <= arr[to] && from<mid) || to >= last) {
arrSort[i] = arr[from];
from++;
} else if ((arr[to]<arr[from] && to<last) || from >= mid) {
arrSort[i] = arr[to];
to++;
}
}
for (int i = first; i<last; i++)
arr[i] = arrSort[i];
}
void mergeSortIter(int array[], int size) {
int *a = new int[size];
for (int i = 1; i <= size; i *= 2) {
showArray(array, size);
for (int j = i; j <= size; j += (2 * i))
mergeSort(array, a, j - i, j, min((j + i), size));
}
}
Rest des Codes
es testen Sie mi 6
[Enter] (6 Anzahl der Elemente), dann Elemente mit Platz für Beispiel 5 6 8 2 1 3
[Eingabe]
Ein Teil des Problems ist Ihre endgültige for-Schleife, wo der Index nur durch zwei geht. Es könnte auch ein tieferes Designproblem geben. – jwimberley