【问题标题】:Search in circular sorted martix and run complexity在循环排序矩阵中搜索并运行复杂性
【发布时间】:2022-01-07 13:50:51
【问题描述】:

该方法给出的 NxN 矩阵总是 2 的幂和一个数字,如果找到 num 则返回 true,例如 4x4 大小:

这是我写的:

public class Search {
public static boolean Search (int [][] matrix, int num) 
{
int value = matrix.length / 2;
int first_quarter_pivot = matrix[value-1][0]; // represents highest number in first quarter
int second_quarter_pivot = matrix[value-1][value]; // represents highest number in second quarter
int third_quarter_pivot = matrix[matrix.length-1][value]; // represents highest number in third quarter
int fourth_quarter_pivot = matrix[matrix.length-1][0]; // represents highest number in fourth quarter
boolean isBoolean = false; 
int i=0;
int j;  



// if the num is not in the range of biggest smallest number it means he can`t be there.    
 if(!(num >= first_quarter_pivot) && (num <= fourth_quarter_pivot)) {
 return false;
}
// if num is one of the pivots return true;
if((num == first_quarter_pivot || (num ==second_quarter_pivot)) 
|| (num == third_quarter_pivot) || (num == fourth_quarter_pivot ))
return true;

// if num is smaller than first pivot it means num is the first quarter,we limit the search to first quarter.
// if not smaller move to the next quarter pivot
if(num < first_quarter_pivot){{
    j =0;
    do
   
        if(matrix[i][j] == num) {
               isBoolean = true;
               break;
               
               
            }
           else if((j == value)) {
                        j = 0;
                        i++;
                    }
                        else if(matrix[i][j] != num){
                         j++;
                        }
                        while(isBoolean != true) ;
                    }
      
              return isBoolean;

            }

    
// if num is smaller than second pivot it means num is the second quarter,we limit the search to second quarter.
// if not smaller move to the next quarter pivot    
if(num < second_quarter_pivot){{
    j = value;// start (0,value) j++ till j=value
    do
    
        if(matrix[i][j] == num) {
               isBoolean = true;
               break;
            }
           else if((j == matrix.length-1)) {
                        j = value;
                        i++;
                    }
                        else if(matrix[i][j] != num){
                         j++;
                        }
                        while(isBoolean != true) ;
               
}
return isBoolean;
}
            
            // if num is smaller than third pivot it means num is the third quarter,we limit the search to third quarter.
// if not smaller move to the next quarter pivot
            
if(num < third_quarter_pivot){{
i = value;              
j = value;// start (0,value) j++ till j=value
    do
    
        if(matrix[i][j] == num) {
               isBoolean = true;
               break;
            }
           else if((j == matrix.length-1)) {
                        j = value;
                        i++;
                    }
                        else if(matrix[i][j] != num){
                         j++;
                        }
                        while(isBoolean != true) ;
           
            }
 return isBoolean;
      
  }     
          // if num is smaller than fourth pivot it means num is the fourth quarter,we limit the search to fourth quarter.
// number must be here because we verfied his existence in the start.
  if(num < fourth_quarter_pivot){
    i = value;              
    j = 0;// start (0,value) j++ till j=value
    do
    
        if(matrix[i][j] == num) {
               isBoolean = true;
               break;
            }
           else if((j == value)) {
                        j = 0;
                        i++;
                    }
                        else if(matrix[i][j] != num){
                         j++;
                        }
                        while(isBoolean != true) ;
 
} 
return isBoolean;    

}

}

我想做什么: 找到想要的号码在哪个季度,然后检查 通过移动 j++ 直到达到极限,比 i++ 移动相同的季度 直到找到 随着每个季度的限制都在变化,我可以t understand if run time complexity is O(n^2) or lower? and will it be better do create one dimensional array and and move on the quarter this way: move right until limit,one down,move left until limit and il 有一个排序数组并且只是二进制搜索

【问题讨论】:

    标签: java arrays performance matrix runtime


    【解决方案1】:

    如果可以将数组映射到矩阵,则可以使用正常的二分查找。

    您可以像这样定义翻译表来实现:

    X = [0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3, ...]
    Y = [0, 1, 1, 0, 2, 3, 3, 2, 2, 3, 3, 2, 0, 1, 1, 0, ...]
    

    最终的程序如下所示。

    static final int MAX_N = 64;
    static final int MAX_NN = MAX_N * MAX_N;
    static final int[] DX = {0, 0, 1, 1};
    static final int[] DY = {0, 1, 1, 0};
    static final int[] X = new int[MAX_NN];
    static final int[] Y = new int[MAX_NN];
    
    static {  // initialize X and Y
        for (int i = 0; i < MAX_NN; ++i) {
            int x = 0, y = 0;
            for (int t = i, f = 0; t > 0; ++f) {
                int mod = t & 3;
                x += DX[mod] << f; y += DY[mod] << f;
                t >>= 2;
            }
            X[i] = x; Y[i] = y;
        }
    }
    
    public static boolean Search(int [][] matrix, int num) {
        int n = matrix.length, nn = n * n;
        int lower = 0;
        int upper = nn - 1;
        while (lower <= upper) {
            int mid = (lower + upper) / 2;
            int value = matrix[X[mid]][Y[mid]];
            if (value == num)
                return true;
            else if (value < num)
                lower = mid + 1;
            else
                upper = mid - 1;
        }
        return false;
    }
    

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 3, 7, 9},
            {6, 4, 15, 11},
            {36, 50, 21, 22},
            {60, 55, 30, 26},
        };
        // case: exists
        System.out.println(Search(matrix, 1));
        System.out.println(Search(matrix, 60));
        System.out.println(Search(matrix, 11));
        // case: not exists
        System.out.println(Search(matrix, 0));
        System.out.println(Search(matrix, 70));
        System.out.println(Search(matrix, 20));
    }
    

    输出:

    true
    true
    true
    false
    false
    false
    

    【讨论】:

    • 你的帮助,不幸的是,这个练习中不允许使用列表,我不知道如何在 atm 中使用它们
    • @anton5450 我将答案更新为仅使用数组。但是,可以处理的矩阵大小限制为MAX_N × MAX_N或更小。
    • 再次感谢,您能解释一下算法是如何工作的吗?第二部分是我理解的二分搜索,但第一部分你为矩阵中的 x 和 y 值创建了 2 个数组,DX 和 DY 是如何使用的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2021-09-16
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多