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

Enter image details here

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

ssis - How do I fix the error unable to enlist Sybase database in distributed transaction? -

c++ - How do I get a multi line tooltip in MFC -