![]() Initially, the search space contains indices 1 through 11. We are interested in the location of the target value in the sequence so we will represent the search space as indices into the sequence. For example, consider the following sequence of integers sorted in ascending order and say we are looking for the number 55: 0 By doing this repeatedly, it will eventually be left with a search space consisting of a single element, the target value. Based on the comparison and because the sequence is sorted, it can then eliminate half of the search space. At each step, the algorithm compares the median value in the search space to the target value. The search space is initially the entire sequence. Binary search maintains a contiguous subsequence of the starting sequence where the target value is surely located. We’ll call the sought value the target value for clarity. In its simplest form, binary search is used to quickly find a value in a sorted sequence (consider a sequence an ordinary array for now). In order to explore it, we’ll first build up a theoretical backbone, then use that to implement the algorithm properly and avoid those nasty off-by-one errors everyone’s been talking about. Discuss this article in the forums Binary search is one of the fundamental algorithms in computer science. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |