Implementation of k-way merge sort.