Given a matrix with m
rows and n
columns, where each entry consists of a pair (a,b)
of integers. No pair appears twice in the matrix.
We would now like to order these pairs, such that for two pairs (a,b)
and (c,d)
which are in the same row, we have that the pair (a,b)
is on the left of (c,d)
if and only if ((a < c) or (a=c and b < d))
, i.e. if the pair is lexicographically ordered according to its entries. Furthermore, for two pairs (a,b)
and (c,d)
in the same column, we have that (a,b)
is below (c,d)
if and only if ((b < d) or (b=d and a < c)
.
If we find a placement such that all rows and columns satisfy the above condition, we call it stable.
It is easy to obtain a stable placement by first sorting all pairs according to the row-condition, which takes O(mn log(mn))
time. Then split this list into m parts p_1,...,p_m
(each consists now of n elements and for each (a,b)
in p_i
, (c,d)
in p_j
it is (a,b) < (c,d) if i < j
). Now sort each part p_i
according to the column-condition, which takes O(n log(n))
per part, i.e. O(mn log(n))
for all parts. Writing the sorted part p_i
into column i
gives a stable placement.
Now consider the special case m=n
, N=n²
. With the above argument we get that a stable placement can be obtained in time O(N log(N))
, which gives an upper bound. Now this poses the question:
What is a lower bound for this problem? Is O(N log(N))
optimal?