Greedy Algorithm with mergesort