Dark
Light
Solarized Light
Solarized

Binary Search Examples:

Java
Python
PHP
C#
C++
TypeScript
▸ Binary Search Quick Review

Binary Search in C++

``````#include &lt;stdio.h&gt;

// Iterative implementation of Binary Search:
int BinarySearchIterative(int arr[], int size, int num) {
int mid, low, high;
low = 0;
high = size - 1;

while(low &lt;= high) {
mid = (int)((low + high) / 2);

printf("l: %d, m:%d, h:%d\n", low, mid, high);

if(num &lt; arr[mid]) {
high = mid - 1;
}
else if(num &gt; arr[mid]) {
low = mid + 1;
}
else {
return mid;
}
}
return -1;
}

int BinarySearchRecursiveAux(int arr[], int num, int low, int high) {
int mid = (int)((low + high) / 2);

if(low &gt;= high) return -1;

if(num &lt; arr[mid]) {
return BinarySearchRecursiveAux(arr, num, low, mid - 1);
}
else if (num &gt; arr[mid]) {
return BinarySearchRecursiveAux(arr, num, mid + 1, high);
}
else {
return mid;
}
}

int BinarySearchRecursive(int arr[], int size, int num) {
return BinarySearchRecursiveAux(arr, num, 0, size - 1);
}

int main() {
// Test array:
int a[] = {1,2,3,4,5,6,7,8,9};
// Get the number of elements in the array:
int s = sizeof(a) / sizeof(a[0]);

// Check the iterative approach:
if(BinarySearchIterative(a, s, 8) &gt; 0) {
printf("Array contains 8\n");
}
else {
printf("Array does not contain 8\n");
}
// Check the recursive approach:
if(BinarySearchRecursive(a, s, 10) &gt; 0) {
printf("Array contains 10\n");
}
else {
printf("Array does not contain 10\n");
}

// exit
return 0;
}
``````

Find any bugs in the code? let us know!