Question

In: Computer Science

In this program, use binary search to search for an element in 2D sorted matrix, the...

In this program, use binary search to search for an element in 2D sorted matrix, the program should include binary search which has two arguments(element to search, and matrix as a List [List])

The Binary search function should return the row and column of the matrix if the element is found. Use a tuple to return the row and column. Return "Not Found" if the element is not exist

Please comment the code

Sample Input:

Case 1: binarysearch(4, [[1, 2, 3],[4, 5, 6],[7, 8, 9]] )

Case 2: binarysearch(10,[[2, 3, 4],[5, 7, 9],[11, 12, 13],[20, 22, 24]]

Sample Output :

Case 1: (1 , 0)

Case 2: Not Found

Solutions

Expert Solution

Python Program:

def binarySearchRecursive(mat, i, jLow, jHigh, ele):
   # Iterating over part of list
   while (jLow <= jHigh):
       # Calculating middle element
       jMid = int((jLow + jHigh) / 2)
       # Element found
       if (mat[i][jMid] == ele):
           return (i, jMid)
       # Left Sub List
       elif (mat[i][jMid] > ele):
           jHigh = jMid - 1
       # Right Sub List
       else:
           jLow = jMid + 1
      
   # If element is not found
   return "Element not found"
      

def binarysearch(ele, mat):
   # Finding number of rows and columns
   rows = len(mat)
   cols = len(mat[0])
   # Checking number of rows
   if (rows == 1):
       return binarySearchRecursive(mat, 0, 0, cols-1, ele)
  
   # Initializing values
   iLow = 0
   iHigh = rows - 1
   # Calculating middle position
   jMid = int(cols / 2)
   # Iterating over array
   while ((iLow + 1) < iHigh):
       # Middle index
       iMid = int((iLow + iHigh) / 2)
       # Comparing values
       if (mat[iMid][jMid] == ele):
           return (iMid, jMid)
       elif (mat[iMid][jMid] > ele):
           iHigh = iMid
       else:
           iLow = iMid
  
   # If element is found
   if (mat[iLow][jMid] == ele):
       return (iLow, jMid)
   elif (mat[iLow + 1][jMid] == ele):
       return (iLow+1, jMid)
   # Searching for element on 1st half of 1st row
   elif (ele <= mat[iLow][jMid - 1]):
       return binarySearchRecursive(mat, iLow, 0, jMid - 1, ele)
   # Searching for element on 2nd half of 1st row
   elif (ele >= mat[iLow][jMid + 1] and ele <= mat[iLow][cols - 1]):
       return binarySearchRecursive(mat, iLow, jMid + 1, cols - 1, ele)
   # Searching for element on 1st half of 2nd row
   elif (ele <= mat[iLow + 1][jMid - 1]):
       return binarySearchRecursive(mat, iLow + 1, 0, jMid - 1, ele)
   # Searching for element on 2nd half of 2nd row
   else:
       return binarySearchRecursive(mat, iLow + 1, jMid + 1, cols - 1, ele)

      
# Testing functions
print(binarysearch(4, [[1, 2, 3],[4, 5, 6],[7, 8, 9]]))
print(binarysearch(10,[[2, 3, 4],[5, 7, 9],[11, 12, 13],[20, 22, 24]]))

____________________________________________________________________________________________

Code Screenshot:

____________________________________________________________________________________________

Sample Run:


Related Solutions

PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your...
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your code should run in O(m + n) where m is the number of rows and n is the number of columns The python file should have a function named binarysearch The function should take two arguments. The first argument of the function will be the element to search for. Second argument of the function will be the matrix as a List[List]. The function should...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...
develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a...
C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a selection sort to organize an array, then find the value using a binary search. Input the array, and a value to find. Output the sorted array and the value if contained in the array. /* * File: main.cpp * Author: * Created on: * Purpose: Binary Search */ //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Random Functions #include <ctime> //Time Library using namespace...
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use...
Problem 1: Write a Java program for traversing a Binary Search Tree in following ways: (use any data structure, library functions) ---------- 20 points i) Depth First Traversal: Inorder, LVR ii) Depth First Traversal: Inorder, RVL iii) Depth First Traversal: Preorder, VLR iv) Depth First Traversal: Preorder, VRL v) Depth First Traversal: Postorder, LRV vi) Depth First Traversal: Postorder, RLV No choice menu required. Sample Input (taken from keyboard, single space separated nodes as found in a complete BFS: top-down,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT