Sign in to follow this  

how to find time complexity of this ..

This topic is 2663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi ,
This program finds a number in a 2dimensional sorted matrix ..
but i can't figure out how to find its time complexity ,,

#include<iostream>
using namespace std;
void find_num(int a[][3] , int row,int col,int curr_row ,int curr_col,int to_find)
{
if(curr_row >= row || curr_col < 0)
{
cout<<"num not found";
return;
}
if(a[curr_row][curr_col] == to_find)
{
cout<<"num found at ::"<<curr_row<<":"<<curr_col;
return;
}
else
{
if(a[curr_row][curr_col] > to_find)
{
find_num( a,4,3,curr_row,curr_col-1,to_find);
}
else
{
find_num( a,4,3,curr_row+1,curr_col,to_find);
}
return;
}



}

int main()
{
int a[4][3] = {{ 1, 2, 10} ,{3, 4, 23},{4, 5, 24} ,{6,7,27}};

int to_find = 7;
find_num(a,4,3,0,2,to_find);
}





Can some1 help ?

[Edited by - Zahlman on August 29, 2010 2:49:28 AM]

Share this post


Link to post
Share on other sites
You would however still have to fix the hard-coded values with its recursive calls before it's anything but O(1). Not to mention you'd need to change the function prototype.
"col" isn't actually used here either.

Share this post


Link to post
Share on other sites

This topic is 2663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this