Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

how to find time complexity of this ..

This topic is 2855 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
Advertisement
Use a [source][/source] tag, and fix the indentation, then I might take a look at it.

Share this post


Link to post
Share on other sites
The time complexity is O(1) as the data set size is hard coded and there is a fixed worst-case time.

Try make your implementation work on arbitrary data sets.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!