0% found this document useful (0 votes)
42 views

Binary Search: Binary Search or Half-Interval Search

Binary search is an algorithm that finds the position of a target value within a sorted array. It works by comparing the target value to the middle element of the array and eliminating half of the remaining elements from further search at each step. This process continues, narrowing down the search space, until either the target value is found or the entire array has been searched. The algorithm executes in logarithmic time, making it very efficient even for large collections, but it requires the array to be sorted. Binary search can be implemented recursively or iteratively by adjusting the search space at each step based on the comparison to the middle element.

Uploaded by

Aafi Qaisar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Binary Search: Binary Search or Half-Interval Search

Binary search is an algorithm that finds the position of a target value within a sorted array. It works by comparing the target value to the middle element of the array and eliminating half of the remaining elements from further search at each step. This process continues, narrowing down the search space, until either the target value is found or the entire array has been searched. The algorithm executes in logarithmic time, making it very efficient even for large collections, but it requires the array to be sorted. Binary search can be implemented recursively or iteratively by adjusting the search space at each step based on the comparison to the middle element.

Uploaded by

Aafi Qaisar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Binary Search:

Binary search or half-interval search algorithm finds the position of a target value
within a sorted array. The binary search algorithm can be classified as
a dichotomy divide-and-conquer search algorithm and executes in logarithmic time.
Binary search is one of the fundamental algorithms in computer science. In order to
explore it, well first build up a theoretical backbone, then use that to implement the
algorithm properly and avoid those nasty off-by-one errors everyones been talking
about.
The binary search algorithm begins by comparing the target value to the value of the
middle element of the sorted array. If the target value is equal to the middle element's
value, then the position is returned and the search is finished. If the target value is less
than the middle element's value, then the search continues on the lower half of the
array; or if the target value is greater than the middle element's value, then the search
continues on the upper half of the array. This process continues, eliminating half of the
elements, and comparing the target value to the value of the middle element of the
remaining elements - until the target value is either found (and its associated element
position is returned), or until the entire array has been searched (and "not found" is
returned).
Example:
Sorted array: L = [1, 3, 4, 6, 8, 9, 11]
Target value: X = 4
Compare X to 6. X is smaller. Repeat with L = [1, 3, 4].
Compare X to 3. X is larger. Repeat with L = [4].
Compare X to 4. X equals 4, so the position is returned.
Characteristics
Every iteration eliminates half of the remaining possibilities. This makes binary searches
very efficient - even for large collections. Our implementation relies on recursion, though
it is equally as common to see an iterative approach.

Binary search requires a sorted collection. This means the collection must either be
sorted before searching, or inserts/updates must be smart. Also, binary searching can
only be applied to a collection that allows random access (indexing).

Algorithm
Algorithm is quite simple. It can be done either recursively or iteratively:
1. get the middle element;
2. if the middle element equals to the searched value, the algorithm stops;
3. otherwise, two cases are possible:
o searched value is less, than the middle element. In this case, go to the
step 1 for the part of the array, before middle element.
o searched value is greater, than the middle element. In this case, go to the
step 1 for the part of the array, after middle element.
Now we should define, when iterations should stop. First case is when searched
element is found. Second one is when subarray has no elements. In this case, we can
conclude, that searched value doesn't present in the array.

Recursive
A straightforward implementation of binary search is recursive. The initial call uses the
indices of the entire array to be searched. The procedure then calculates an index
midway between the two indices, determines which of the two subarrays to search, and
then does a recursive call to search that subarray. Each of the calls is tail recursive, so
a compiler need not make a new stack frame for each call. The
variables imin and imax are the lowest and highest inclusive indices that are searched.

int binary search (int A[], int key, int imin, int imax)
{
// test if array is empty
if (imax < imin)
// set is empty, so return value showing not found

return KEY_NOT_FOUND;
else
{
// calculate midpoint to cut set in half
int imid = midpoint (imin, imax);
// three-way comparison
if (A[imid] > key)
// key is in lower subset
return binary_search(A, key, imin, imid - 1);
else if (A[imid] < key)
// key is in upper subset
return binary_search(A, key, imid + 1, imax);
else
// key has been found
return imid;
}
}
Iterative
The binary search algorithm can also be expressed iteratively with two index limits that
progressively narrow the search range.
int binary_search (int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imin <= imax)
{
// calculate the midpoint for roughly equal partition
int imid = midpoint (imin, imax);
if (A[imid] == key)
// key found at index imid
return imid;
// determine which subarray to search
else if (A[imid] < key)
// change min index to search upper subarray
imin = imid + 1;
else

// change max index to search lower subarray


imax = imid - 1;
}
// key was not found
return KEY_NOT_FOUND;}

You might also like