# Binary Search

How would i setup a binary search on an array with, oh, lets say 600 slots?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
What type of data? Integer, String.
Define slots? Is that array elements?
0
Commented:
Binary search is pretty and the question gives me pause.  I hesitate to give more info or actual "code" on this because of the outside chance some of the question askers are studens trying to get "too much" help.

If the array items are not already sorted I would use 'Quicksort' code to sort your array.  There are several VB Quicksort Array source codes easily found and I believe even on this site..

The binary search I believe you want would be described as starting with the entire dataset of (600 items) as your "master group"

We determine if it has more than 2 items in the current group and if so then divide it in half to find a "center" item and compare to the center item of the group if your center item is BELOW < your SOUGHT item then the top half becomes the new master search tree group.  If it is instead ABOVE > the other lower half is the new master group and the process repeats until your item matches the center dividing item or your current group contains less than three items in which case those remaining items are checked as "centers" were above and one is chosen and should hopefully match or else a "no match" condition is returned.

0
Commented:
make that "pretty simply"
0
Author Commented:
Well, i was doing the devide in half thing, but it got to the point where, what if the number is greater the 300.
600 \ 2 = 300
so then arrayfind > 300 so what would i do??
0
Commented:
Okay so for our example the records values just happen to equal their record numbers - so if your value is greater than 300 and you compare to item 300 then your remaining MASTER SET OF RECORDS your "Possibles" are the TOP 300 records 301 through 600.  You then divide that 300 records which is more than two in half which is 150 records and compare to the record at 301+150 or "451" and say your searching for "376" so this time you are UNDER the center item so again you would now search the LOWER half of the split which is not 301 through 450 or only 150 records.  Divide by two again and you get only 75 records so this time compare to record 301+75 or 376 and voila a match!  Got it?
0
Commented:
make that is NOW 301 through 450 or only 150 records
0
Commented:
in short if you are over you adjust the lower limit start point as when we went from 1 to 301 and whe you are under you adjust the added value as we did from 150 to 75 until you hit your item or get to where you only have one or two items which are too few to have lower upper and center item any longer then you break down and just flat compare them.  So is there any credit for me in all of this?  <Smile>
0

Experts Exchange Solution brought to you by