Binary Search Algorithm

4 Comments

Program to implement Binary Search Algorithm

#include <iostream.h> template<class T> int binarySearch(T a[], int n, T& x) { int left = 0; // left end of segment int right = n – 1; // right end of segment while (left <= right) { int middle = (left + right)/2; // middle of segment if (x == a[middle]) return middle; if (x […]