implemented k-way merge