This is going to be a rather large question from me since it requires deep and thorough understanding of the problem and also various approaches taken up to now for the optimal solution.
For this matter, I will create a blog later on this as it requires little bit of time from my side to write up the contents and so forth.So, I will provide my blog link once it's ready.
Nevertheless, I am posting this so that we can get started discussing.
First thing first:
The problem is as follows:
Write an efficient algorithm that searches for a value in an n x m table (two-dimensional >array). This table is sorted along the rows and columns — that is,
Table[i][j] ≤ Table[i][j + 1] Table[i][j] ≤ Table[i + 1][j].
Here to be noted that each rows and columns are monotonically sorted i.e. each rows and columns are sorted in non-decreasing (lexicographical) order.
For the actual problem and a nice discussion on this problem, click on the following link:
Note:This post has part 1,2 and 3.
Hong Shen, from Griffith University, Australia in December 27, 1995 published the optimal solution for this problem to be O(mlog(2n/m)) for a serial algorithm.
Hong Shen-- Generalized Optimal Matrix Search
Since then a lots of forum discussions, blogs, and online and offline articles and publications have been made on this, but as of today, as far as I know, O(((log(n))2) is the best claimed on this article with Improved Binary Search.
Even though the detailed algorithm has not been provided.
Now, I have done different variations of solutions on this problem in the hope to bring the optimal solution to be lower than the current best(I think)
However, I am not that good at the analysis of time complexities of algorithms.
The following is just one of them. As we come up with decisions I will keep providing others and thus come up with the best with all your helps.
Here's the first one for you to analyze the time complexity
/* NIAZ MOHAMMAD
IntPol2d.cpp
1. Find the interpolated mid along column
2. Compare key with each elements in row-wise
along the found mid column
3. IF failed
DO
RECURSIVE call to IntPol2d
a. IF(key > a[row][mid])
THEN SET l = mid + 1 and d = row - 1;
b. ELSE set r = mid -1 and u = row;
*/
bool intPol2d(int mat[][6], int M, int N, int target, int l, int u, int r, int d, int &targetRow, int &targetCol,int &count) {
count++;
if (l > r || u > d) return false;
if (target < mat[u][l] || target > mat[d][r]) return false;
int mid = l + ((target - mat[u][l])*(r-l))/(mat[d][r]-mat[u][l]);
int row = u;
while (row <= d && mat[row][mid] <= target)
{
if (mat[row][mid] == target)
{
targetRow = row;
targetCol = mid;
return true;
}
row++;
}
return intPol2d(mat, M, N, target, mid+1, u, r, row-1, targetRow, targetCol,count) ||
intPol2d(mat, M, N, target, l, row, mid-1, d, targetRow, targetCol,count);
}
If you require the whole executable code, please let me know.
Thanks for your patience and help and see ya on the discussion board :
Note: @ Admins or moderators, let me know if this is not the right place for this type of lengthy question, then I will move to my blog.