Well, friend, we're both wrong.

The algorithm will find 6 just fine. It will choose 3 as the middle
element. Since 6 is bigger, it will throw away the subarray

1 2
2 3

and check the other 3 subarrays.  When it checks

6 7
7 8

It will find the 6 on the first try.  I just verified this by running
the code.

Second, I should have solved the recurrence.  You're right that it's ~
n^1.6  .  Throwing away a quarter of the _elements_ isn't good enough
because that number is n^2.

The algorithm is only sublinear in the number of elements, which of
course is worse than the standard algorithm that starts at the lower
left corner and steps along a jagged path of max length ~2n.

But I should have seen that you can split the L-shaped remaining piece
of each subarray into only TWO pieces rather than 3.  So the recursive
calls should have been:

    return (x < a[mi][mj]) ?
      search(a, x, i0, mi - 1, j0, j1, i, j) ||
      search(a, x, mi, i1, j0, mj - 1, i, j)
    :
      search(a, x, i0, mi, mj + 1, j1, i, j) ||
      search(a, x, mi + 1, i1, j0, j1, i, j);

With this change the recurrence is more complicated, but it should be
faster than n^1.6 .  I'm not going to try it tonight...

On Sep 27, 8:19 am, Nikhil Jindal <fundoon...@yahoo.co.in> wrote:
> @saurabh.nsit:
>
> Consider the following array:
>
> 1 2 6 7
> 2 3 7 8
> 4 5 8 9
> 5 7 9 10
>
> And the item to be searched is 6. As I understand it, using your approach
> you will search 6 in only the second and third row, which will not give the
> correct solution.
> Hope this clears a few doubts.
>
> @Gene:
> Analysing the complexity of ur algo:
>
> T(n) = 3*T(n/2) + O(1)
>
> which is n^(log_2(3)) = n^1.6.
>
> Cheers
> Nikhil Jindal
>
> On Sun, Sep 26, 2010 at 11:14 PM, saurabh singh <saurabh.n...@gmail.com>wrote:
>
>
>
>
>
> > As you mentioned ultimately element to be searched should either be in row
> > 'i' (ahead of [i,i] element) or in row i+1 (before [i+1,i+1] element). Since
> > each row contain numbers in sorted order so u can do binary search on these
> > two rows and ultimately the complexity will be O(logn) only
>
> >  On Sun, Sep 26, 2010 at 7:34 PM, Nikhil Jindal 
> > <fundoon...@yahoo.co.in>wrote:
>
> >>  On Tue, Sep 21, 2010 at 6:05 PM, saurabh singh 
> >> <saurabh.n...@gmail.com>wrote:
>
> >>> solution 1:
> >>> use concept of quad-tree and do binary search in that tree
>
> >>> solution 2:
> >>> do binary search on major diagonal. ultimately u will narrow down to
> >>> search for element in  2 rows. in these two rows again do binary search.
>
> >> How do you narrow down to two rows? Please explain.
> >> By searching on the diagonal, you get two elements such that one is lesser
> >> than the number being searched for and the next is greater. let them be 
> >> i,i,
> >> and i+1,i+1.
>
> >> So you remove the array from 0,0 to i,i and from i+1,i+1 to n-1,n-1. But
> >> the number could be anywhere in the rest of the array
>
> >>> any solution will lead you to O(log(n)) time
>
> >>> On Tue, Sep 21, 2010 at 5:10 PM, jagadish <jagadish1...@gmail.com>wrote:
>
> >>>> Hi all,
> >>>> Given a 2d array which is sorted row wise and column wise as well,
> >>>> find a specific element in it in LESS THAN O(n).
> >>>> PS: an O(n) solution would involve skipping a column or a row each
> >>>> time from the search and moving accordingly.
> >>>> Solution less than O(n) is desirable!
>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "Algorithm Geeks" group.
> >>>> To post to this group, send email to algoge...@googlegroups.com.
> >>>> To unsubscribe from this group, send email to
> >>>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups
> >>>>  .com>
> >>>> .
> >>>> For more options, visit this group at
> >>>>http://groups.google.com/group/algogeeks?hl=en.
>
> >>> --
> >>> Thanks & Regards,
> >>> Saurabh
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "Algorithm Geeks" group.
> >>> To post to this group, send email to algoge...@googlegroups.com.
> >>> To unsubscribe from this group, send email to
> >>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups
> >>>  .com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/algogeeks?hl=en.
>
> >> Please access the attached hyperlink for an important electronic 
> >> communications 
> >> disclaimer:http://dce.edu/web/Sections/Standalone/Email_Disclaimer.php
>
> >> --
>
> >> You received this message because you are subscribed to the Google Groups 
> >> "Algorithm Geeks" group.
>
> >> To post to this group, send email to algoge...@googlegroups.com.
>
> >> To unsubscribe from this group, send email to 
> >> algogeeks+unsubscr...@googlegroups.com 
> >> <algogeeks%2bunsubscr...@googlegroups.com>.
>
> >> For more options, visit this group 
> >> athttp://groups.google.com/group/algogeeks?hl=en.
>
> > --
> > Thanks & Regards,
> > Saurabh
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algoge...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups 
> > .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> Please access the attached hyperlink for an important electronic 
> communications 
> disclaimer:http://dce.edu/web/Sections/Standalone/Email_Disclaimer.php

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to