This question is not a homework, it is just out of personal interest and mainly my curiosity
My professor talked about this question for a little while during class, but he didn't talk much about this. And below is the question:
Given an m x n matrix A whose integer elements are sorted along the horizontal and vertical direction respectively. I need to develop a recursive program to search if a query value a is in A. Discuss the time complexity of your design.
So I thought about this for a while. My first approach is to check the base case: the first element and last element
check if first element > item check if last element < item
item is what I want to find
This is the imaginary matrix: ( x can be any number, but this matrix is sort vertically and horizontally)
first x x x x
x x x x x
x x mid x x
x x x x x
x x x x last
After I check the base case and make sure the "item" I want to find is inside the range of this matrix, I don't know if it is alright to check from the "mid" in the matrix ( like binary search). If item < mid , then focus on left half. If item > mid, then focus on right half.
But, then I tried to make a matrix like below and my "item" is 10
1 2 3 4 5
2 4 7 8 9
3 6 10 11 12
If I follow the way I said before: since 10 is larger than the middle "7", I focus on right part. then I check "8", since 10 is larger than "8", I search for right part. But 10 is not in the right ...
Can anyone give me idea or insight how to solve this question? Thanks a lot