The function sorts an array of instances of the contact structure by the sname field; for fairly large arrays, there is a problem with stack overflow due to recursion in 5-6 lines. How can this sort be implemented without recursion?
void MergeSort(contact* buf, int first, int last) { if (first >= last - 1) return; int mid = (first + last) / 2; MergeSort(buf, first, mid); MergeSort(buf, mid, last); contact * mas = new contact[last - first]; for (int i = first; i < last; ++i) mas[i - first] = buf[i]; int l = 0, r = mid - first; for (int i = first; i < last; ++i) { if (l == mid - first) buf[i] = mas[r++]; else if (r == last - first) buf[i] = mas[l++]; else if (strcmp(mas[l].sname, mas[r].sname) < 0) buf[i] = mas[l++]; else buf[i] = mas[r++]; } delete[] mas; }
log nrecursion depth - you have to try it ... - Harry