void mergeSortPart (int * A, int p, int r) { // Сортировка части массива if (p<r) { int q=(p+r)/2; mergeSortPart (A, p, q); mergeSortPart (A, q+one, r); merge (A, p, q, r); } }
void mergeSortPart(iter_t beg, iter_t end, int size){ if(size <= 1) return; iter_t mid = beg; for(int i = 0; i < size/2 ; ++i){ mid = mid->m_next; } // size/2 + (size+1)/2 = size // store beg's prev in advance, which is unchangable, to get a new beg iter_t header = beg->m_prev; mergeSortPart(beg, mid, size/2); // similiarly iter_t pre_mid = mid->m_prev; mergeSortPart(mid, end, (size+1)/2); // restore merge(header->m_next, pre_mid->m_next, end); }
void mergeSort (int * A, int length) { // Сортировка всего массива методом слияния mergeSortPart (A, zero, length-one); }
void mergeSort(iter_t beg, iter_t end, int size, cmp_t compare){ cmp = compare; mergeSortPart(beg, end, size); }