# A Binary Search Algorithm

**Version Compatibility:*** Visual Basic 6 *

This snippet of code is a binary search algorithm. It is the concept of splitting your searchable array in two discarding the half that does not have the element for which you are looking.

You place your items in an array and sort them. Then you simply get the middle element and test if it is <, >, or = to the element for which you are searching. If it is less than, you discard the greater half, get the middle index of the remaining elements and do it again.

Binary search divides your problem in half everytime you execute your loop. Depending on what methods are available to you for comapring two values, you have a result of either log(n) or O(log n).

Though written for VB, this can easily be translated into any language. For this snippet, you need a textBox, label and command button control.

**Instructions:** Copy the declarations and code below and paste directly into your VB project.

**Declarations:**

**Code:**