lecture slide V-38 : Correction
void mergesort_help (int A[], int lo, int hi) {
if (lo - hi > 1)
int mid = (lo + hi)/2;
......
}
change (lo - hi > 1) to (hi - lo > 1)
lecture slide V-39 : A more intuitive implementation of merge
void merge(int A[], int lo, int mid, int hi) {
int fir, sec, index;
int tempArray[MAX_SIZE];
fir = lo;
sec = mid + 1;
index = 0;
while (fir <= mid && sec <= hi) {
if (A[fir] < A[sec]) {
tempArray[index] = A[fir];
index++;
fir++;
}
else {
tempArray[index] = A[sec];
index++;
sec++;
}
}
// now either (fir > mid) or (sec > hi) or both
// thus at most one of these while loops is executed
while (fir <= mid) {
tempArray[index] = A[fir];
index++;
fir++;
}
while (sec <= hi) {
tempArray[index] = A[sec];
index++;
sec++;
}
// copy the temporary array back into the original array
for (int i = 0; i < hi - lo; i++) {
A[lo + i] = tempArray[i];
}
}