如何矩阵已所有m行进行排序n列排序排序amxn?矩阵、amxn

2023-09-11 02:06:53 作者:不期待。再重来

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

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

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

下面是一个例子:

  [1,4,7,10]

 [2,5,8,11]

 [3,6,9,12]
 
有2xN矩阵,第一行是编号,第二行是元素,用matlab如何让矩阵按第二行的元素升序排列

是输入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 的< 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)

 
精彩推荐