📜  在已排序的二维矩阵中搜索(以行主要顺序存储)

📅  最后修改于: 2021-09-16 10:56:17             🧑  作者: Mango

给定一个整数“K”和一个按行排序的二维矩阵,即矩阵具有以下属性:

  • 每行中的整数从左到右排序。
  • 每行的第一个整数大于前一行的最后一个整数。

任务是找出矩阵中是否存在整数“K”。如果存在,则打印“找到”,否则打印“未找到”。
例子:

Input: mat = {
  {1,   3,  5,  7},
  {10, 11, 16, 20},
  {23, 30, 34, 50}}
  K = 3
Output: Found

Input: mat = {
  {1,   3,  5,  7},
  {10, 11, 16, 20},
  {23, 30, 34, 50}}
  K = 13
Output: Not found

我们已经讨论了排序矩阵中搜索元素的一种实现。在这篇文章中,提供了一个更好的实现。
方法:这个想法是使用分而治之的方法来解决这个问题。

  • 首先应用二分搜索来查找特定行,即“K”位于该行的第一个和最后一个元素之间。
  • 然后对该行应用简单的二分搜索以查找该行中是否存在“K”。

下面是上述方法的实现:

C++
// C++ program to find whether
// a given element is present
// in the given 2-D matrix
#include 
using namespace std;
 
#define M 3
#define N 4
 
// Basic binary search to
// find an element in a 1-D array
bool binarySearch1D(int arr[], int K)
{
    int low = 0;
    int high = N - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
 
        // if element found return true
        if (arr[mid] == K)
            return true;
 
        // if middle less than K then
        // skip the left part of the
        // array else skip the right part
        if (arr[mid] < K)
            low = mid + 1;
        else
            high = mid - 1;
    }
 
    // if not found return false
    return false;
}
 
// Function to search an element
// in a matrix based on
// Divide and conquer approach
bool searchMatrix(int matrix[M][N], int K)
{
    int low = 0;
    int high = M - 1;
    while (low <= high) {
        int mid = low + (high - low) / 2;
 
        // if the element lies in the range
        // of this row then call
        // 1-D binary search on this row
        if (K >= matrix[mid][0]
            && K <= matrix[mid][N - 1])
            return binarySearch1D(matrix[mid], K);
 
        // if the element is less then the
        // starting element of that row then
        // search in upper rows else search
        // in the lower rows
        if (K < matrix[mid][0])
            high = mid - 1;
        else
            low = mid + 1;
    }
 
    // if not found
    return false;
}
 
// Driver code
int main()
{
    int matrix[M][N] = { { 1, 3, 5, 7 },
                         { 10, 11, 16, 20 },
                         { 23, 30, 34, 50 } };
    int K = 3;
    if (searchMatrix(matrix, K))
        cout << "Found" << endl;
    else
        cout << "Not found" << endl;
}


Java
// Java program to find whether
// a given element is present
// in the given 2-D matrix
 
public class GFG {
 
    static final int M = 3;
    static final int N = 4;
 
    // Basic binary search to
    // find an element in a 1-D array
    static boolean binarySearch1D(int arr[], int K)
    {
        int low = 0;
        int high = N - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
 
            // if element found return true
            if (arr[mid] == K) {
                return true;
            }
 
            // if middle less than K then
            // skip the left part of the
            // array else skip the right part
            if (arr[mid] < K) {
                low = mid + 1;
            }
            else {
                high = mid - 1;
            }
        }
 
        // if not found return false
        return false;
    }
 
    // Function to search an element
    // in a matrix based on
    // Divide and conquer approach
    static boolean searchMatrix(int matrix[][], int K)
    {
        int low = 0;
        int high = M - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
 
            // if the element lies in the range
            // of this row then call
            // 1-D binary search on this row
            if (K >= matrix[mid][0]
                && K <= matrix[mid][N - 1]) {
                return binarySearch1D(matrix[mid], K);
            }
 
            // if the element is less then the
            // starting element of that row then
            // search in upper rows else search
            // in the lower rows
            if (K < matrix[mid][0]) {
                high = mid - 1;
            }
            else {
                low = mid + 1;
            }
        }
 
        // if not found
        return false;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int matrix[][] = { { 1, 3, 5, 7 },
                           { 10, 11, 16, 20 },
                           { 23, 30, 34, 50 } };
        int K = 3;
        if (searchMatrix(matrix, K)) {
            System.out.println("Found");
        }
        else {
            System.out.println("Not found");
        }
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to find whether a given
# element is present in the given 2-D matrix
 
M = 3
N = 4
 
# Basic binary search to find an element
# in a 1-D array
def binarySearch1D(arr, K):
    low = 0
    high = N - 1
    while (low <= high):
        mid = low + int((high - low) / 2)
 
        # if element found return true
        if (arr[mid] == K):
            return True
 
        # if middle less than K then skip
        # the left part of the array
        # else skip the right part
        if (arr[mid] < K):
            low = mid + 1
        else:
            high = mid - 1
 
    # if not found return false
    return False
 
# Function to search an element in a matrix
# based on Divide and conquer approach
def searchMatrix(matrix, K):
    low = 0
    high = M - 1
    while (low <= high):
        mid = low + int((high - low) / 2)
 
        # if the element lies in the range
        # of this row then call
        # 1-D binary search on this row
        if (K >= matrix[mid][0] and
            K <= matrix[mid][N - 1]):
            return binarySearch1D(matrix[mid], K)
 
        # if the element is less then the
        # starting element of that row then
        # search in upper rows else search
        # in the lower rows
        if (K < matrix[mid][0]):
            high = mid - 1
        else:
            low = mid + 1
 
    # if not found
    return False
 
# Driver code
if __name__ == '__main__':
    matrix = [[1, 3, 5, 7],
              [10, 11, 16, 20],
              [23, 30, 34, 50]]
    K = 3
    if (searchMatrix(matrix, K)):
        print("Found")
    else:
        print("Not found")
 
# This code is contributed by
# Shashank_Sharma


C#
// C# program to find whether
// a given element is present
// in the given 2-D matrix
using System;
     
class GFG
{
 
    static int M = 3;
    static int N = 4;
 
    // Basic binary search to
    // find an element in a 1-D array
    static bool binarySearch1D(int []arr, int K)
    {
        int low = 0;
        int high = N - 1;
        while (low <= high)
        {
            int mid = low + (high - low) / 2;
 
            // if element found return true
            if (arr[mid] == K)
            {
                return true;
            }
 
            // if middle less than K then
            // skip the left part of the
            // array else skip the right part
            if (arr[mid] < K)
            {
                low = mid + 1;
            }
            else
            {
                high = mid - 1;
            }
        }
 
        // if not found return false
        return false;
    }
 
    // Function to search an element
    // in a matrix based on
    // Divide and conquer approach
    static bool searchMatrix(int [,]matrix, int K)
    {
        int low = 0;
        int high = M - 1;
        while (low <= high)
        {
            int mid = low + (high - low) / 2;
 
            // if the element lies in the range
            // of this row then call
            // 1-D binary search on this row
            if (K >= matrix[mid,0]
                && K <= matrix[mid,N - 1])
            {
                return binarySearch1D(GetRow(matrix,mid), K);
            }
 
            // if the element is less then the
            // starting element of that row then
            // search in upper rows else search
            // in the lower rows
            if (K < matrix[mid,0])
            {
                high = mid - 1;
            }
            else
            {
                low = mid + 1;
            }
        }
 
        // if not found
        return false;
    }
    public static int[] GetRow(int[,] matrix, int row)
    {
        var rowLength = matrix.GetLength(1);
        var rowVector = new int[rowLength];
 
        for (var i = 0; i < rowLength; i++)
        rowVector[i] = matrix[row, i];
 
        return rowVector;
    }
     
    // Driver code
    public static void Main(String []args)
    {
        int [,]matrix = { { 1, 3, 5, 7 },
                        { 10, 11, 16, 20 },
                        { 23, 30, 34, 50 } };
        int K = 3;
        if (searchMatrix(matrix, K)) {
            Console.WriteLine("Found");
        }
        else {
        Console.WriteLine("Not found");
        }
    }
}
 
// This code contributed by Rajput-Ji


PHP
= $matrix[$mid][0] && $K <= $matrix[$mid][$GLOBALS['N'] - 1])
            return binarySearch1D($matrix[$mid], $K);
 
// if the element is less then the
// starting element of that row then
// search in upper rows else search
// in the lower rows
        if ($K < $matrix[$mid][0])
            $high = $mid - 1;
        else
            $low = $mid + 1;
    }
 
// if not found
    return False;
}
 
// Driver code
 
$matrix = array([1, 3, 5, 7],
                [10, 11, 16, 20],
                [23, 30, 34, 50]);
$K = 3;
$M = 3;
$N = 4;
if (searchMatrix($matrix, $K))
echo "Found";
else
echo "Not found";
 
// This code is contributed by
// Srathore
?>


Javascript


输出:
Found

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程