• Want to help us with this year's BoS Trials?
    Let us know before 30 June. See this thread for details
  • Looking for HSC notes and resources?
    Check out our Notes & Resources page

Binary Search Confusion (1 Viewer)

sig

Member
Joined
Feb 25, 2004
Messages
111
Gender
Male
HSC
2004
After reading Sam Davis's algorithm on Binary Search which reads

Code:
BEGIN BinarySearch
  Set Low to 1
  Set High to number of items in list
  Set Found to False
  Get ItemToFind
  WHILE High > Low AND Found = False
    Set Middle to INT((Low + High)/2)
    IF ItemToFind < Item(Middle) THEN
      Set High to Middle - 1
    ELSEIF ItemToFind = Item(Middle) THEN
      Set Found to True
    ELSE
      Set Low to Middle + 1
    ENDIF
  ENDWHILE
  IF Found = True THEN
    Display "Found"
  ELSE
    Display "Not Found"
  ENDIF
END BinarySearch
If I had 5 Items and the item I was trying to find was number 4 in the list...
After First Part the Low would be Middle + 1 Therefore Number 4

2nd Pass would involved middle being [(4 + 5)/2] which would be 4.5 so rounded to 5 I presume?

This is when I get confused since the target item is smaller then the middle it would make High to Middle - 1 (Thus 4)

But then both High and Low would equal 4 and the number would not actually be found

Can someone please clear the confusion

Thanks

ps. sorry about the >>> I dunno how to indent
 
Last edited by a moderator:

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
Its truncated, so the decimal is removed and not rounded up. So the middle for the second pass would be 4 which means it will find it since it is the middle
 

Wild Dan Hibiki

teh sex0r
Joined
Feb 28, 2004
Messages
649
Gender
Undisclosed
HSC
2004
acmilan1987 said:
Its truncated, so the decimal is removed and not rounded up. So the middle for the second pass would be 4 which means it will find it since it is the middle
yep yep, i was just about to say that... :) seriously though, good thing i opened this thread
 

Seraph

Now You've done it.......
Joined
Sep 26, 2003
Messages
897
Gender
Male
HSC
N/A
Question

Set High to Middle - 1

is this ideally a more optimized way performing a binary search? where we move it one up or one down , and is this vital in the binary search algorithm?

Furthermore when there is a multiple choice question giving a set of numbers and saying how many searches will ti take to find blah blah number , wont the answer chagne depending on whether we use this method or not?
 

Freedom_Dragon

The 36th Dragon
Joined
Oct 11, 2003
Messages
154
Location
Behind a door that will never open.
Gender
Undisclosed
HSC
N/A
IF Target < Array_Item(Middle) THEN
Upper=Middle-1
ELSE
Lower=Middle+1
Most important. Very Vital.
In order to determine where the target is located (lower half or upper half) u would need to know "Low" & "High" + them together to obtain "Middle".
The target exist either in the lower half or upper half. One half is discarded, thus getting closer to finding the target .
A new "Low" Lower=Middle+1 or "High" Upper=Middle-1 & "Middle" is obtained.This process is repeated until the target is found.

This is the main concept. Besides... what other binary search method could u use? U wouldnt use a linear search for an ordered array.
REPEAT Middle=Int(Lower+Upper)/2
IF Array_Item(Middle)=Target THEN
Found=True
FoundPosition=Middle
ELSE
IF Target < Array_Item(Middle) THEN
Upper=Middle-1
ELSE
Lower=Middle+1

Hope That Helped
 
Last edited:

Seraph

Now You've done it.......
Joined
Sep 26, 2003
Messages
897
Gender
Male
HSC
N/A
ehh never the mind

thanks anyway
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top