Algorithm 想知道对排序矩阵使用二进制搜索逻辑时的时间复杂度吗

Algorithm 想知道对排序矩阵使用二进制搜索逻辑时的时间复杂度吗,algorithm,recursion,data-structures,matrix,recursive-datastructures,Algorithm,Recursion,Data Structures,Matrix,Recursive Datastructures,我在寻找使用二进制搜索逻辑对排序矩阵进行优化的最佳方法 使用堆栈溢出中“Michal Sznajder”建议的逻辑 但在计算时间复杂度和递推关系上存在困难。 有人能帮我吗 虽然我使用的是二进制搜索逻辑,但我假设它不是T(n)=log(m+n) 它是T(n)=log(log(log(mxn))还是log(log(mxn)) 代码可能不是最好的标准或优化,但我刚刚开始编写 /* 1, 4, 7, 10, 13, 2, 5, 8, 11,

我在寻找使用二进制搜索逻辑对排序矩阵进行优化的最佳方法

使用堆栈溢出中“Michal Sznajder”建议的逻辑

但在计算时间复杂度和递推关系上存在困难。 有人能帮我吗

虽然我使用的是二进制搜索逻辑,但我假设它不是T(n)=log(m+n)

它是T(n)=log(log(log(mxn))还是log(log(mxn))

代码可能不是最好的标准或优化,但我刚刚开始编写

/*
 1,    4,      7,      10,     13,
 2,    5,      8,      11,     14,
 3,    6,      9,      12,     15,
 16,   17,     18,     19,     20,
 21,   22,     23,     24,     25,
 26,   27,     28,     29,     30

 a ..... b ..... c
 . .     . .     .
 .   1   .   2   .
 .     . .     . .
 d ..... e ..... f
 . .     . .     .
 .   3   .   4   .
 .     . .     . .
 g ..... h ..... i

a, c, g < i --> if not there is no element 

a, b, d < e
b, c, e < f
d, e, g < h
e, f, h < i

Left Top    :   Right Top
-----------Mid--------------
Left Bottom :   Right Bottom

*/

public int SearchSortedMatrixInLogNByM(int[,] SortedMatix, int elementToSearch, int rowStPos, int colStPos, int rowEndPos, int colEndPos)
{
    // Step 0. Check for basic validations.
    if (SortedMatix == null)
    {
        throw new Exception("Matrix is empty");
    }

    // Step 1. Use divide and conquer and to get the middle element.
    int resultNode = 0;
    int rowMidPos = (rowStPos + rowEndPos) / 2;
    int colMidPos = (colStPos + colEndPos) / 2;

    // Step 2. Mid element in Recursive Sub Matrix. For e.g in above example if it is 'e', 'f','h','i' then found.
    if (SortedMatix[rowMidPos, colMidPos] == elementToSearch || SortedMatix[rowMidPos, colEndPos] == elementToSearch ||
        SortedMatix[rowEndPos, colMidPos] == elementToSearch || SortedMatix[rowEndPos, colEndPos] == elementToSearch)
    {
        return elementToSearch;
    }

    // Step 3. Terminate the sub matrix iteration when the element is not found in the 2 X 2 matrix. 
    if ((rowStPos == rowMidPos || colStPos == colMidPos) && SortedMatix[rowStPos, colStPos] != elementToSearch)
    {
        return 0;
    }

    // Step 4. Left Top Sub Matrix.
    if (resultNode == 0 && elementToSearch < SortedMatix[rowMidPos, colMidPos])
    {
        resultNode = SearchSortedMatrixInLogNByM(SortedMatix, elementToSearch, rowStPos, colStPos, rowMidPos, colMidPos);
    }

    // Step 5. Right Top Sub Matrix.
    if (resultNode == 0 && elementToSearch < SortedMatix[rowMidPos, colEndPos])
    {
        resultNode = SearchSortedMatrixInLogNByM(SortedMatix, elementToSearch, rowStPos, colMidPos, rowMidPos, colEndPos);
    }

    // Step 6. Left bottom Sub Matrix.
    if (resultNode == 0 && elementToSearch < SortedMatix[rowEndPos, colMidPos])
    {
        resultNode = SearchSortedMatrixInLogNByM(SortedMatix, elementToSearch, rowMidPos, colStPos, rowEndPos, colMidPos);
    }

    // Step 7. Right bottom Sub Matrix.
    if (resultNode == 0 && elementToSearch < SortedMatix[rowEndPos, colEndPos])
    {
        resultNode = SearchSortedMatrixInLogNByM(SortedMatix, elementToSearch, rowMidPos, colMidPos, rowEndPos, colEndPos);
    }

    return resultNode;
}

public void SearchSortedMatrixTest()
{
    int[,] SortedMatix = {{ 1,    4,      7,      10,     13,},
                            { 2,    5,      8,      11,     14,},
                            { 3,    6,      9,      12,     15,},
                            { 16,   17,     18,     19,     20,},
                            { 21,   22,     23,     24,     25,},
                            { 26,   27,     28,     29,     30}};

    //SearchSortedMatrixInNLogN(AssendMatix, 21);

    StringBuilder strBldr = new StringBuilder();

    strBldr.Append("\n 1  : " + SearchSortedMatrixInLogNByM(SortedMatix, 1, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 2  : " + SearchSortedMatrixInLogNByM(SortedMatix, 2, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 3  : " + SearchSortedMatrixInLogNByM(SortedMatix, 3, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 4  : " + SearchSortedMatrixInLogNByM(SortedMatix, 4, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 5  : " + SearchSortedMatrixInLogNByM(SortedMatix, 5, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 6  : " + SearchSortedMatrixInLogNByM(SortedMatix, 6, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 7  : " + SearchSortedMatrixInLogNByM(SortedMatix, 7, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 8  : " + SearchSortedMatrixInLogNByM(SortedMatix, 8, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 9  : " + SearchSortedMatrixInLogNByM(SortedMatix, 9, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 10 : " + SearchSortedMatrixInLogNByM(SortedMatix, 10, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));

    strBldr.Append("\n 11 : " + SearchSortedMatrixInLogNByM(SortedMatix, 11, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 12 : " + SearchSortedMatrixInLogNByM(SortedMatix, 12, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 13 : " + SearchSortedMatrixInLogNByM(SortedMatix, 13, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 14 : " + SearchSortedMatrixInLogNByM(SortedMatix, 14, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 15 : " + SearchSortedMatrixInLogNByM(SortedMatix, 15, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 16 : " + SearchSortedMatrixInLogNByM(SortedMatix, 16, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 17 : " + SearchSortedMatrixInLogNByM(SortedMatix, 17, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 18 : " + SearchSortedMatrixInLogNByM(SortedMatix, 18, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 19 : " + SearchSortedMatrixInLogNByM(SortedMatix, 19, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 20 : " + SearchSortedMatrixInLogNByM(SortedMatix, 20, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));

    strBldr.Append("\n 21 : " + SearchSortedMatrixInLogNByM(SortedMatix, 21, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 22 : " + SearchSortedMatrixInLogNByM(SortedMatix, 22, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 23 : " + SearchSortedMatrixInLogNByM(SortedMatix, 23, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 24 : " + SearchSortedMatrixInLogNByM(SortedMatix, 24, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 25 : " + SearchSortedMatrixInLogNByM(SortedMatix, 25, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 26 : " + SearchSortedMatrixInLogNByM(SortedMatix, 26, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 27 : " + SearchSortedMatrixInLogNByM(SortedMatix, 27, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 28 : " + SearchSortedMatrixInLogNByM(SortedMatix, 28, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 29 : " + SearchSortedMatrixInLogNByM(SortedMatix, 29, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 30 : " + SearchSortedMatrixInLogNByM(SortedMatix, 30, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));

    strBldr.Append("\n\n Example 2:\n");

    SortedMatix = new int[,] {{ 1,    4,      7,},
                                { 2,    5,      8,},
                                { 3,    6,      9}};

    strBldr.Append("\n 1  : " + SearchSortedMatrixInLogNByM(SortedMatix, 1, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 2  : " + SearchSortedMatrixInLogNByM(SortedMatix, 2, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 3  : " + SearchSortedMatrixInLogNByM(SortedMatix, 3, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 4  : " + SearchSortedMatrixInLogNByM(SortedMatix, 4, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 5  : " + SearchSortedMatrixInLogNByM(SortedMatix, 5, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 6  : " + SearchSortedMatrixInLogNByM(SortedMatix, 6, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 7  : " + SearchSortedMatrixInLogNByM(SortedMatix, 7, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 8  : " + SearchSortedMatrixInLogNByM(SortedMatix, 8, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));
    strBldr.Append("\n 9  : " + SearchSortedMatrixInLogNByM(SortedMatix, 9, 0, 0, SortedMatix.GetLength(0) - 1, SortedMatix.GetLength(1) - 1));

    MessageBox.Show("Element(s) Search in Sorted Matrix and the result(s) are " + strBldr.ToString());          
}
/*
1,    4,      7,      10,     13,
2,    5,      8,      11,     14,
3,    6,      9,      12,     15,
16,   17,     18,     19,     20,
21,   22,     23,     24,     25,
26,   27,     28,     29,     30
A.BC
. .     . .     .
.   1.2.
.     . .     . .
DEF
. .     . .     .
.   3.4.
.     . .     . .
GH我
a、 c,g如果没有元素
a、 b,d 1,  2,  4, 10
 3,  6,  7, 12
 5,  8,  9, 14
11, 13, 15, 16