You get a bonus - 1 coin for daily activity. Now you have 1 coin

Binary search

Lecture



When the search for some element must be carried out in an ordered sequence in ascending or descending order, then we apply the binary (binary) search algorithm. The method uses the “divide and conquer” strategy, namely: a given sequence is divided into two equal parts and the search is performed in one of these parts, which then also divides in two, and so on until the presence of the desired element or its absence is detected. Using this operation, halving the search area each time, is only possible on the basis of the fact that the sequence elements are pre-ordered. Having found the middle element (it is not difficult to do this, knowing the number of elements in the array), and comparing its value with the desired one, we can confidently say where the required element is located relative to the middle element.

Further, we will assume that the elements of the array are arranged in ascending order, since there is no significant difference in how they are ordered: ascending or descending value. We also denote the boundaries of the search zone by left and right - the leftmost and rightmost elements, respectively.

The progress of the algorithm, divided into stages, is as follows:

  1. the search area (in the first step, the entire array is divided into two), by defining its middle (mid) element;
  2. the middle element is compared with the desired key, the result of this comparison will be one of three cases:
    • key <mid. The element at the middle (right ← mid-1) becomes the rightmost edge of the search area;
    • key> mid. The leftmost border of the search area becomes the next element after the middle (left ← mid + 1);
    • key = mid. The values ​​of the average and the required elements coincide, therefore the element is found, the algorithm is completed.
  3. if there is no element left to check, the algorithm is completed, otherwise the transition to step 1 is performed.

The table below shows a specific integer array, and step-by-step execution of a binary search algorithm applied to its elements. To save space in the table, left, right and mid are replaced by a, b and c.

  Binary search

There is a sequence of integers arranged in ascending order, and the key is the number 16. Initially, the boundary elements are the elements with numbers 1 and 9, and the values ​​1 and 81. The number of the middle element is calculated, for which the formula is usually used (right + left) / 2, or left + (right-left) / 2 (further, the second formula will be used in the programs as the most resistant to overflows). After comparison, it turns out that the desired element is less than the average, and therefore the subsequent search is performed in the left part of the sequence. The algorithm continues to run in a similar way, until the desired element is found at step 4.

It should be noted that much less time is required here than if we used linear search, but unlike linear search, binary works only with arrays whose elements are ordered, which undoubtedly gives it negative specificity.

C ++ program code:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15
sixteen
17
18
nineteen
20
21
22
23
24
25
26
27
28
29
thirty
31
#include "stdafx.h"
#include <iostream>
using namespace std;
const int N = 10;
// binary search
int BinarySearch (int A [], int key)
{
int left = 0, right = N, mid;
while (left <= right)
{
mid = left + (right-left) / 2;
if (key <A [mid]) right = mid-1;
else if (key> A [mid]) left = mid + 1;
else return mid;
}
return -1;
}
// main function
void main ()
{
setlocale (LC_ALL, "Rus");
int i, key;
int A [N];
cout << "Search Item>"; cin >> key; // enter key
cout << "Source array:";
for (i = 0; i <N; i ++) // fill and output array
{A [i] = N * i; cout << A [i] << ""; }
if (BinarySearch (A, key) == - 1) cout << "\ nItement not found";
else cout << "\ nItem number:" << BinarySearch (A, key) +1;
system ("pause >> void");
}

Pascal program code:

one
2
3
four
five
6
7
eight
9
ten
eleven
12
13
14
15
sixteen
17
18
nineteen
20
21
22
23
24
25
26
27
28
29
program BinSearch;
uses crt;
const N = 10;
type Arr = array [1..N] of integer;
var mid, left, right, key, i: integer;
A: Arr;
{binary search}
function BinarySearch (A: Arr; key: integer): integer;
begin
left: = 1; right: = N;
while left <= right do
begin
mid: = left + (right-left) div 2;
if (key <A [mid]) then right: = mid-1
else if (key> A [mid]) then left: = mid + 1
else begin BinarySearch: = mid; exit; end;
end;
BinarySearch: = - 1;
end;
{main program block}
begin
write ('search element>'); read (key); {key entry}
write ('Source array:');
for i: = 1 to N do {fill and output array}
begin A [i]: = N * i; write (A [i], ''); end;
writeln;
if (BinarySearch (A, key) = - 1) then write ('Element not found')
else write ('Element number:', BinarySearch (A, key));
end.

In the program, it would be possible to implement an array check for the presence of elements in it, but since it is filled, regardless of the user, by a strictly defined number of constant values, this is not necessary.

In the case when the first mid value coincides with the key, then it is considered that the algorithm was executed in its best time O (1). In average and worst case, the algorithm has a running time of O (log n ), which is much faster than a linear search that requires linear time.

created: 2014-11-30
updated: 2021-03-13
250



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithms

Terms: Algorithms