CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    Smile 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/././/

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    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

  3. #3
    Join Date
    Feb 2010
    Posts
    2

    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

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    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
  •  





Click Here to Expand Forum to Full Width

Featured