如何矩阵已所有m行进行排序n列排序排序amxn? [英] How to sort a m x n matrix which has all its m rows sorted and n columns sorted?

查看:365
本文介绍了如何矩阵已所有m行进行排序n列排序排序amxn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于具有m行和n列,其中每一个被排序的矩阵。如何将整个矩阵有效排序?

我知道它运行在O(MN日志(分钟(M,N))的解决方案。我正在寻找一个更好的解决方案。

这是我所知道的方法基本上是采用2行/ COLS的时间和适用合并操作。

下面是一个例子:

  [1,4,7,10]

 [2,5,8,11]

 [3,6,9,12]
 

是输入martix有每一行和列进行排序。

预期成果是:

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

另外一个例子:

 [[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]]
 

解决方案

我不认为你可以在任何速度比Ω(这样做的明尼苏达州的日志(分( M N 的)),至少在一般情况下

假设(不失一般性),该的&其中; N 的。那么你的矩阵如下所示:

每个圈是一个矩阵条目,每个箭头表示一个公知的顺序关系(入口在箭头的源比所述条目在箭头的目的地小)。

要排序的矩阵,我们必须解决所有未知​​的顺序关系,其中一些显示在这里的灰色框:

排序所有这些箱子需要:

  

2Σ<子>的 K 的&LT; M 的Ω( K 的日志的 K 的)+( N 的 - M + 1)Ω( M 的日志的 M 的)

     

= 2Ω( M 的²登录的 M 的)+( N 的 - M 的+ 1)Ω( M 的日志的 M 的)

     

=Ω( M N 的日志的 M 的)

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]]

解决方案

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:

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:

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)

这篇关于如何矩阵已所有m行进行排序n列排序排序amxn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆