Improve merge sort with less memory usage