|
-
February 10th, 2010, 04:56 AM
#1
plz can ny1 help me plz urgent
Suppose that you are given a sorted sequence of distinct integers {a1 , a2 , . . . , an }. Give
an O(lg n) algorithm to determine whether there exists an index i such at ai = i. For example, in
{−5, −3, 3, 5, 7}, a3 = 3. In {2, 3, 4, 6, 7} there is no such i.
plz can any1 tell me a proper algorthnm for this plz/././/
-
February 10th, 2010, 06:37 AM
#2
Re: plz can ny1 help me plz urgent
O(lg n) and sorted data suggest a binary search.
Say you split the array in half. You get down to an index i and a sequence integer ai. If they're equal you have a hit. If not you must decide which half to split in half next. You do that by comparing ai with i. If ai > i there's no way there can be a hit to the right and if ai < i there's no way there can be a hit to the left. So you split accordingly and continue doing so until there's a hit or no further splitting is possible
-
February 10th, 2010, 10:29 AM
#3
Re: plz can ny1 help me plz urgent
thanks a lot dude
dude can u tell me sample algirthm for it jst a procedure is enough dude......ny way thanks for the help dude
-
February 10th, 2010, 10:48 AM
#4
Re: plz can ny1 help me plz urgent
I think you should try yourself otherwise you don't learn anything. Just slightly modify the standard recursive binary search algorithm for an element in an array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|