sorting - Algorithm to sort pairs of numbers -
I am stuck with a problem and I need some help from SO bright mind. I have n pairs of unsigned integers. I need to sort them. The vector ending of the pair should be sorted from the first number in the first number in each pair, and in each pair the second should be non-narrowly second . There can be first and second elements that swap each other in each pair, sometimes there is no solution, so I have to throw an exception.
Example:
In pairs: 1 5 7 1 3 8 5 6 Added outside: 1 7 & lt; - Swapped 1 5 6 5 & lt; - Swap 8 3 & lt; - Swap ^ ^ It is impossible to create a solution without swapping pairs, so we swap pairs (7, 1), (3, 8) and (5, 6) and Produce results. Or
in pairs: 1 5 6 9 out: is not possible Another example that shows how 'Sorting Yuges' is not the first solution < / P>
in pairs: 1 4 2 5 Added outside: 1 4 5 2 thanks
O (n log n) solution
Comments
Post a Comment