typename SortedList<T, Pred>::Node* SortedList<T, Pred>::merge_sort_helper(typename SortedList<T, Pred>::Node* current, unsigned chunk_size, const Sorter &sorter) { if (current != tail_ && chunk_size > 1) { SortedList<T, Pred>::Node* temp = current; advanceNode(temp, chunk_size / 2); // Mergesort the left, mergesort the right, and merge the results! return(merge_lists(merge_sort_helper(current, chunk_size / 2, sorter), chunk_size / 2, merge_sort_helper(temp, chunk_size - (chunk_size / 2), sorter), chunk_size - (chunk_size / 2), sorter)); } return(current); }
std::vector<int> merge_sort_helper(std::vector<int> &array, int begin, int last){ /* Split array in parts and merge sorted. Split happens around middle element that is calculating on each revursive call. merge happens in bottom-up manner. */ if(begin >= last){ return array; } int middle = (last+begin)/2; merge_sort_helper(array, begin, middle); merge_sort_helper(array, middle+1, last); return merge(array, begin, middle, middle+1, last); }
void SortedList<T, Pred>::merge_sort(Sorter sorter) { // TODO: STUB! // Short circuit if there's nothing to sort. if (size_ <= 1U) return; merge_sort_helper(head_->Next, size_, sorter); }
std::vector<int> merge_sort(const std::vector<int> &array){ // wrapper for recuirsive sort. std::vector<int> result = array; return merge_sort_helper(result, 0, array.size()-1); }