Monday, May 5, 2008

binary search in vb.net

vb.net allows you to use arrays (zero based) to hold data.
One feature that is a very handy and fast method of the array class is : binarysearch.
It returns the position of an element within an array (one dimensional).

This is how it works:
Say for instance you declare an array with 3 items:
dim cars() as string = ("ford", "chevy", "dodge")

Now say that for some reason you want to find the position of the element "chevy" out of the cars() array.

vb.net has a very handy method for that: binarysearch

Here is how it works:

dim findindex as integer = array.binarysearch(cars,"chevy")
findindex would return 1. If you were to search "ford" it would return 0, hence vb.net arrays are zero-based.

dim findindex as integer = array.binarysearch(cars,"toyota") would return -1 (not found)

Well, that's it for now.
c ya next time

No comments: