for
loop that returns when/if element is foundNOTE: binary search can only be implemented on a sorted list of items. if elements are not sorted already, need to sort them first.
Binary search algorithm can be implemented in two ways
general steps:
- element to be searched is x = 4
mid
of array (i.e. arr[(low + high)/2] = 6
)x == mid
, return mid.x > mid
, compare x
with middle element of elements on right side of mid
low
to low = mid + 1
x
with middle element of elements to left side of mid
high
to high = mid - 1
x = 4
is founddo until the pointers low and high meet each other
mid = (low + high)/2
if (x == arr[mid]) {
return mid
} else if (x > arr[mid]) { // x is on right side
low = mid + 1
} else { // x is on left side
high = mid - 1
}
binarySearch(arr, x, low, high) {
if (low > high) {
return false
} else {
mid = (low + high) / 2
if (x == arr[mid]) {
return mid
} else if (x > arr[mid]) { // x is right side
return binarySearch(arr, x, mid + 1, high)
} else { // x is on left side
return binarySearch(arr, x, low, mid - 1)
}
}
}
Python
def binarySearch(array, x, low, high):
# Repeat until the pointers low and high meet each other
while low <= high:
mid = low + (high - low)//2
if array[mid] == x:
return mid
elif array[mid] < x:
low = mid + 1
else:
high = mid - 1
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x = 4
result = binarySearch(array, x, 0, len(array)-1)
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
C++
#include <iostream>
using namespace std;
int binarySearch(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int x = 4;
int n = sizeof(array) / sizeof(array[0]);
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
}
Java
class BinarySearch {
int binarySearch(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void main(String args[]) {
BinarySearch ob = new BinarySearch();
int array[] = { 3, 4, 5, 6, 7, 8, 9 };
int n = array.length;
int x = 4;
int result = ob.binarySearch(array, x, 0, n - 1);
if (result == -1)
System.out.println("Not found");
else
System.out.println("Element found at index " + result);
}
}
Python
def binary_search(array, x, low, high):
if high >= low:
mid = low + (high - low)//2
# If found at mid, then return it
if array[mid] == x:
return mid
# Search the left half
elif array[mid] > x:
return binary_search(array, x, low, mid - 1)
# Search the right half
else:
return binary_search(array, x, mid + 1, high)
return -1
array = [3, 4, 5, 6, 7, 8, 9]
x = 4
result = binary_search(array, x, 0, len(array) - 1)
if result != -1:
print("Element is present at index " + str(result))
else:
print("Not found")
C++
#include <iostream>
using namespace std;
int binarySearch(int array[], int x, int low, int high) {
if (high >= low) {
int mid = low + (high - low) / 2;
// If found at mid, then return it
if (array[mid] == x)
return mid;
// Search the left half
if (array[mid] > x)
return binarySearch(array, x, low, mid - 1);
// Search the right half
return binarySearch(array, x, mid + 1, high);
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int x = 4;
int n = sizeof(array) / sizeof(array[0]);
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
}
Java
class BinarySearch {
int binarySearch(int array[], int x, int low, int high) {
if (high >= low) {
int mid = low + (high - low) / 2; // TODO: why add low too?
if (array[mid] == x) {
return mid;
} else if (array[mid] > x) {
return binarySearch(array, x, low, mid - 1);
} else {
return binarySearch(array, x, mid + 1, high);
}
}
return -1;
}
public static void main(String args[]) {
BinarySearch ob = new BinarySearch();
int array[] = { 3, 4, 5, 6, 7, 8, 9 };
int x = 4;
int result = ob.binarySearch(array, x, 0, array.length - 1);
if (result == -1) {
System.out.println("Not found");
} else {
System.out.println("Element found at index " + result);
}
}
}
O(1)
O(log n)
O(log n)
O(1)
Java
, .Net
, C++ STL