• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Binary Search

How would i setup a binary search on an array with, oh, lets say 600 slots?
0
cdc_sickle
Asked:
cdc_sickle
  • 6
1 Solution
 
LewyCommented:
Please be more specific.
What type of data? Integer, String.
Define slots? Is that array elements?
0
 
InformativeCommented:
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
 
InformativeCommented:
make that "pretty simply"
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
cdc_sickleAuthor 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
 
InformativeCommented:
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
 
InformativeCommented:
make that is NOW 301 through 450 or only 150 records
0
 
InformativeCommented:
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
 
InformativeCommented:
Thanks.  If you have any problems with your code not doing what you need post your code and I will help you debug it.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now