merge sort with pairs matching