C
#include <stdio.h>
int binarySearch(int[], int, int);
int main()
{
int myList[] = {1, 3, 5, 7, 9};
int len = sizeof(myList) / sizeof(myList[0]);
printf("%d\n", binarySearch(myList, 3, len)); // 1
printf("%d\n", binarySearch(myList, -1, len)); //-1
return 0;
}
int binarySearch(int list[], int item, int len)
{
int low = 0;
int high = len;
while (low <= high)
{
int mid = (low + high)/2;
int guess = list[mid];
if (guess == item)
{
return mid;
}
else if (guess > item)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return -1; //number not found
}
C++
#include "iostream"
using namespace std;
void binarySearch(int data_array[], int element, int len)
{
int low = 0;
int high = len;
while (low <= high)
{
int mid = (low + high)/2;
int guess = data_array[mid];
if (guess == element)
{
cout<<"Element found at "<<mid<<" th index"<<endl ;
return ;
}
else if (guess > element)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
cout<<"Element Not Found"<<endl ;
return ; //number not found
}
int main()
{
int data_array[] = {2,10,23,44,100,121};
int length = sizeof(data_array) / sizeof(int);
binarySearch(data_array, 3, length) ; // not found case
binarySearch(data_array, 2, length) ; // found at corner case
binarySearch(data_array, 44, length) ; //found at middle case
return 0;
}
C++11
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
template <typename T>
int binary_search(const std::vector<T>& list, const int& item) {
int low = 0;
int high = list.size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
T guess = list[mid];
if (guess == item) {
return mid;
}
if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
// this function returns pointer to the found element rather than array index
template <typename T>
const T* binary_search2(const std::vector<T>& list, const T& item) {
const T* low = &list.front();
const T* high = &list.back();
while (low <= high) {
// "guess" is the element in the middle between "high" and "low"
const T* guess = low + ((high - low) / 2);
if (*guess == item)
return guess;
if (*guess > item) {
high = guess - 1;
} else {
low = guess + 1;
}
}
return nullptr;
}
int main() {
std::vector<int> my_list = {1, 3, 5, 7, 9};
const int* binary_search2_result = binary_search2(my_list, 9);
const int* binary_search2_null = binary_search2(my_list, 4); // test finding element that is not in the list
cout << "Binary search for number 3: " << binary_search(my_list, 3) << endl;
cout << "Binary search 2 for number 9 (memory address): " << binary_search2_result << endl;
cout << "Binary search 2 for number 9 (value): " << *binary_search2_result << endl;
if (binary_search2_null == nullptr) {
cout << "4 was not found in the list" << endl;
}
return 0;
}
C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var myList = new List<int> { 1, 3, 5, 7, 9 };
Console.WriteLine(BinarySearch(myList, 3)); // => 1
Console.WriteLine(BinarySearch(myList, -1)); // => null gets printed as an empty string
}
private static int? BinarySearch(IList<int> list, int item)
{
var low = 0;
var high = list.Count() - 1;
while (low <= high)
{
var mid = (low + high) / 2;
var guess = list[mid];
if (guess == item) return mid;
if (guess > item)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return null;
}
}
}
JAVA
public class BinarySearch {
// has to return boxed integer in order to comfort to interface defined in the book
private static Integer binarySearch(int[] list, int item) {
int low = 0;
int high = list.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int guess = list[mid];
if (guess == item) {
return mid;
}
if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
public static void main(String[] args) {
int[] myList = {1, 3, 5, 7, 9};
System.out.println(binarySearch(myList, 3)); // 1
System.out.println(binarySearch(myList, -1)); // null
}
}
JS
'use strict';
function binary_search(list, item) {
let low = 0;
let high = list.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
let guess = list[mid];
if (guess === item) {
return mid;
}
if (guess > item) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
const my_list = [1, 3, 5, 7, 9];
console.log(binary_search(my_list, 3)); // 1
console.log(binary_search(my_list, -1)); // null
Python
def binary_search(list, item):
# low and high keep track of which part of the list you'll search in.
low = 0
high = len(list) - 1
# While you haven't narrowed it down to one element ...
while low <= high:
# ... check the middle element
mid = (low + high) // 2
guess = list[mid]
# Found the item.
if guess == item:
return mid
# The guess was too high.
if guess > item:
high = mid - 1
# The guess was too low.
else:
low = mid + 1
# Item doesn't exist
return None
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, 3)) # => 1
# 'None' means nil in Python. We use to indicate that the item wasn't found.
print(binary_search(my_list, -1)) # => None