improve sort algorithm for test cases