17

Given a matrix with m rows and n columns, each of which are sorted. How to efficiently sort the entire matrix?

I know a solution which runs in O(m n log(min(m,n)). I am looking for a better solution.

The approach that I know basically takes 2 rows/cols at a time and applies merge operation.

Here is an example:

[[1,4,7,10],

 [2,5,8,11],

 [3,6,9,12]]

is the input martix which has every row and column sorted.

Expected output is:

[1,2,3,4,5,6,7,8,9,10,11,12]

Another example:

[[1, 2, 3, 3, 4, 5, 6, 6, 7, 7],

 [1, 2, 4, 6, 7, 7, 8, 8, 9,10],

 [3, 3, 4, 8, 8, 9,10,11,11,12],

 [3, 3, 5, 8, 8, 9,12,12,13,14]]
digEmAll
  • 56,430
  • 9
  • 115
  • 140
Anil Katti
  • 1,313
  • 1
  • 14
  • 26

3 Answers3

48

I don't think you can do it any faster than Ω(m n log(min(m, n)), at least not in the general case.

Suppose (without loss of generality) that m < n. Then your matrix looks like this:

a matrix with rows and columns sorted

Each circle is a matrix entry and each arrow indicates a known order relation (the entry at the source of the arrow is smaller than the entry at the destination of the arrow).

To sort the matrix, we must resolve all the unknown order relations, some of which are shown in the grey boxes here:

the order relations remaining to be resolved

Sorting all of these boxes takes:

2 Σk < m Ω(k log k) + (n - m + 1) Ω(m log m)

= 2 Ω(m² log m) + (n - m + 1) Ω(m log m)

= Ω(m n log m)

Community
  • 1
  • 1
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • +1 For the best presented answer I've seen to any question. Also looks correct to me, but that paled in to insignificance. – Orbling Nov 25 '10 at 19:06
  • Wow! Thanks for the explanation. It looks sound. I did not understand a couple of thing: 1. Where did the second term, (n - m + 1) Ω(m log m) comes from? And, 2. I am wondering if there is a tighter upper bound for Σk < m Ω(k log k). Something tighter than Ω(m² log m)? – Anil Katti Nov 25 '10 at 19:47
  • The (*n* − *m* + 1) Ω(*m* log *m*) comes from the diagonal grey boxes that span the matrix from bottom to top: each such box contains *m* elements, and there are (*n* − *m* + 1) of them. As for your other question, Ω is a *lower* bound, not an upper bound. – Gareth Rees Nov 25 '10 at 19:52
  • A good diagram is worth a thousands words, too bad I can't give it a good 10 upvotes to make it up :) – Matthieu M. Nov 25 '10 at 20:06
  • @Gareth - perfect. I got the second term's role. But, are you sure Σk < m Ω(k log k) is Ω(m² log m)? In any case, the summation is dominated by the second term and I agree that the lower bound for this problem is Ω(m n log m). Thanks a ton for this beautiful answer! – Anil Katti Nov 25 '10 at 20:21
  • *x* log *x* is smooth, so Σ *k* log *k* is close to ∫ *x* log *x* dx. – Gareth Rees Nov 25 '10 at 20:29
0

If elements are integers within a certain range k where K=o(mn), we can use count sort with extra space to achieve O(mn), otherwise the mnlog(min(m,n)) is the best we can do.

Daniel
  • 1
0

By creating a Binary Search Tree, we can achieve this in O(mn) time. Take the last element from the first column (element 3 in the example mentioned above), make it as a root. Right nodes will be the n greater elements of that last row and left node will be the one above element ie. the (m-1)th or the 1st element from the second last row. Similarly for this element, the right nodes will be the n elements of that row. Again m-2 will be the left element and all the n elements in it's row will be the right elements. Similarly moving forward we'll have a binary search tree created in O(mn) time. This is O(mn) because we are not searching while inserting, it's a simple insert while traversing by shifting the root node pointer. Then inorder traversal of this BST will be done which will also be O(mn) time.

Maggy
  • 1
  • The procedure you describe guarantees _left nodes_ will be no larger and _right nodes_ will be no smaller than their parent. It does _not_ necessarily build a BST - consider the second example (first element of second column smaller last of first = root) or similarly a₁₁=1, a₁₂=2, a₂₁=3, a₂₂=4 . – greybeard Jan 01 '17 at 12:31