Sunday, May 9, 2010

Super Fast Record Search in C#

Searching for a piece of data in your tables is a common task in almost every single application whether it is a desktop or web application. If you want to look for an item in a relatively small set of records most likely you use some type of record iteration like a foreach loop. We all have done some type of search similar to the one i mentioned.

The problem comes when the amount of information stored in your database begins to increase, and then it grows exponetially! At this point if you have not plan properly you will have to come up with ingenous ideas on how to speed up the data processing and retrieval. Most of this ideas include throwing more hardware power, indexing and the kitchen sink.

Since I am certain that at some point in your life you are going to run into this issue, well here it is. A search that can tackle a large number of record in a very short amount of time, or at least i think so :-\

In this exercise we will create a set of 1 million records, the records are in a circular array that have been shifted "n" positions to the right. The task at hand is to find the amount of spaces in the array that the first element was shifted to the right. No need to worry about the amount of iterations in the array, just the current position.

The keyword here is "BinarySearch" The BinarySearch allows us to search a one dimensional sorted array for an specific value. It uses a binary type of search algorithm.




No comments:

Post a Comment