# Thread: Logic behind Binary Search?

1. ## Logic behind Binary Search?

Hi,
I was recently asked as to what is the logic behind Binary Logic..and I must say the question made no sense to me..maybe one of you can help me..Thanks..
Asp

2. The logic behind the search is that if you have an ordered collection of items, you can search for a given item by dividing that collection in half each time until you find what you are looking for.

3. aspand, just do a google search for "binary search tree". You'll get loads of hits, some have interactive examples as well.

4. A binary search returns the item being searched for with the minimum number of steps. You start at the middle, if the key is greater then you divide the second half and do the same thing with it.
Code:
```key = 12

First step:
marker
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Second Step:
marker
|
11 12 13 14 15 16 17 18 19 20

Third Step:
marker
|
11 12 13 14```
In three steps a binary search returned the correct value. This isn't so great with a list of 20 values, but when the list gets to be huge, such as one million items, a binary search will find the correct value with no more than 20 steps. This is very very fast.

-Prelude