簡體   English   中英

在二維排序數組中搜索元素

[英]Searching an element in a 2D sorted array

我必須編寫一個代碼(作為練習),它接收一個二維(正方形)按行和按列排序的數組和一個元素,並返回 true 是該元素存在於數組中。

當我聽到“排序”時,首先想到的是二分查找,但后來我意識到每行中的最后一個元素不一定小於下一行中的第一個元素。

所以,我發現最好的復雜度是 O(n),並編寫了以下代碼:

 public static boolean findN(int[][] a, int x) {
    if (a.length == 0 || a[0].length == 0 || x > a[a.length - 1][a[0].length - 1] || x < a[0][0]) {
        return false;
    }
    int LastRow = a.length - 1, Lastcol = a[0].length - 1, row = 0, col = 0;

    while (row <= LastRow) {
        if (a[row][col] == x) {
            return true;
        } else if (col < Lastcol) {
            col++;
        } else {
            col = 0;
            row++;
        }
    }
    return false;
}

數組示例:

int [] [] arr = {{1,2,7,30}
                        {2,4,18,50}
                        {3,6,19,90}
                        {4,7,20,91}}
  • 在意識到最好的復雜度是 O(n) 之后,我用谷歌搜索了這個問題,所以我幾乎可以肯定我是對的(盡管有些人聲稱他們可以在 O(log(n)) 中完成),但是我真的嗎?
  • 歡迎任何其他想法和改進,提前謝謝大家!

幾個月前我遇到了一個類似的問題,這里是我發現在 O(logN + logM) 中工作的代碼[假設數組按行和列排序]。

[...] 但是我意識到每行中的最后一個元素不一定小於下一行中的第一個元素。 - 在這種情況下,您無法實現 O(logn) 復雜度。

簡單的二分搜索:

static void binarySearch(int mat[][], int i, int j_low, int j_high, int x) { 
    while (j_low <= j_high) { 
        int j_mid = (j_low + j_high) / 2; 

        // Element found 
        if (mat[i][j_mid] == x) { 
            System.out.println ( "Found at (" + i  + ", " + j_mid +")"); 
            return; 
        } 

        else if (mat[i][j_mid] > x) 
            j_high = j_mid - 1; 

        else
            j_low = j_mid + 1; 
    } 

    System.out.println ( "Element no found"); 
} 

核心邏輯:

static void sortedMatrixSearch(int mat[][], int n, int m, int x) { 
    // Single row matrix 
    if (n == 1) { 
        binarySearch(mat, 0, 0, m - 1, x); 
        return; 
    } 

    // Do binary search in middle column. 
    // Condition to terminate the loop when the 
    // 2 desired rows are found 
    int i_low = 0; 
    int i_high = n - 1; 
    int j_mid = m / 2; 
    while ((i_low + 1) < i_high) { 
        int i_mid = (i_low + i_high) / 2; 

        // element found 
        if (mat[i_mid][j_mid] == x) { 
            System.out.println ( "Found at (" + i_mid +", " + j_mid +")"); 
            return; 
        } 

        else if (mat[i_mid][j_mid] > x) 
            i_high = i_mid; 

        else
            i_low = i_mid; 
    } 

    // If element is present on  
    // the mid of the two rows 
    if (mat[i_low][j_mid] == x) 
        System.out.println ( "Found at (" + i_low + "," + j_mid +")"); 
    else if (mat[i_low + 1][j_mid] == x) 
        System.out.println ( "Found at (" + (i_low + 1)  + ", " + j_mid +")"); 

    // Ssearch element on 1st half of 1st row 
    else if (x <= mat[i_low][j_mid - 1]) 
        binarySearch(mat, i_low, 0, j_mid - 1, x); 

    // Search element on 2nd half of 1st row 
    else if (x >= mat[i_low][j_mid + 1] && x <= mat[i_low][m - 1]) 
    binarySearch(mat, i_low, j_mid + 1, m - 1, x); 

    // Search element on 1st half of 2nd row 
    else if (x <= mat[i_low + 1][j_mid - 1]) 
        binarySearch(mat, i_low + 1, 0, j_mid - 1, x); 

    // search element on 2nd half of 2nd row 
    else
        binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x); 
} 

驅動方式:

public static void main (String[] args) { 
    int n = 4, m = 5, x = 8; 
    int mat[][] = {{0, 6, 8, 9, 11}, 
                   {20, 22, 28, 29, 31}, 
                   {36, 38, 50, 61, 63}, 
                   {64, 66, 100, 122, 128}}; 

    sortedMatrixSearch(mat, n, m, x); 
} 

希望這可以幫助。 祝你好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM