In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. In particular, fractional cascading speeds up binary searches for the same value in multiple arrays. In 1957, William Wesley Peterson published the first method for interpolation search. In 1946, John Mauchly made the first mention of binary search as part of the Moore School lecture, a seminal and foundational college course in computing. In 1962, Hermann Bottenbruch exhibited an ALGOL 60 implementation of binary search that put the comparison for equality at the end, increase the average number of iterations by one, but reduce to one the number of comparisons per iteration.

If the target value is less than the component, the search continues in the lower half of the array. If the target value is greater than the component, the search continues in the upper half of the array. This is the case for other search algorithms based on comparisons, as while they may work faster on some target values, the average performance over all components is worse than binary search. In terms of iterations, no search algorithm that works only by comparing components can show better average and worst-case performance than binary search.

```
/*
Petar 'PetarV' Velickovic
Algorithm: Binary Search
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 1000001
using namespace std;
typedef long long lld;
int n, x;
int niz[MAX_N];
//Binarna pretraga elementa u sortiranom nizu
//Slozenost: O(log n)
inline int b_search(int left, int right, int x)
{
int i = left;
int j = right;
while (i < j)
{
int mid = (i+j)/2;
if (niz[mid] == x) return mid;
if (niz[mid] < x) i = mid+1;
else j = mid-1;
}
if (niz[i] == x) return i;
return -1;
}
int main()
{
n = 5, x = 4;
niz[0] = 1;
niz[1] = 2;
niz[2] = 3;
niz[3] = 4;
niz[4] = 5;
printf("%d\n",b_search(0, n-1, x));
return 0;
}
```