Ciao a tutti,
sto cercando di programmare mergesort in c++ seguendo un certo schema a ricorsione.
codice:
template<class T>
113 void Sort<T>::mergeSortSubrange(std::vector<T>* elements, size_t p, size_t r)
114 {
115
116 // If subrange contains just one or no element, there is nothing to do.
117 if (r <= p) return;
118 std::vector<T>& A = *elements; // Alias for convenience.
119 std::vector<T> X; // The first part of the vector A, X[p,...,m]
120 std::vector<T> Y; // The second part of the vector A, Y[m+1,...r]
121 int m = (r-1)/2;
122
123 // fill the vector X with A´s elements X[p,...,m] = A[p,....m]
124 for (int i = 0; i <= m; i++)
125 X.push_back(A[i]);
126 // call recursively the algorithmus so long as the size X =1
127 if ( m >= 1)
128 mergeSortSubrange(&X,p,m);
129 // fill the vector Y with A´s elements A[m+1,...r]
130 for (size_t j = m+1; j <= r; j++)
131 Y.push_back(A[j]);
132 // call recursively the algorithmus so long as the size of Y =1
133 if (r-m >= 2)
134 mergeSortSubrange(&Y,m,r);
}
Il programma non é ancora finito.Comunque per prima cosa sto cercando di dividere il vettore in 2 vettori.(non ordinati).
Fino a riga 129 va tutto bene quando incomincio ad armeggiare con il 2 array che dovrebbe contenere la 2 parte del vettore, allora ho da qualche parte un ciclo infinito ed il programma non si chiude piü..
e non divide l´array in due parti sempre piü piccole...