Enter target value and click "Start" to begin search
C
int binarySearch(int arr[], int n, int target) { int left = 0, right = n -1;
while (left <= right) { int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid +1;
else right = mid -1;
} return-1;
}
C++
int binarySearch(int arr[], int n, int target) { int left = 0, right = n -1;
while (left <= right) { int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid +1;
else right = mid -1;
} return-1;
}
Java
int binarySearch(int arr[], int target) { int left = 0, right = arr.length -1;
while (left <= right) { int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid +1;
else right = mid -1;
} return-1;
}
Go
func binarySearch(arr []int, target int) int{ left, right := 0, len(arr)-1 for left <= right { mid := left + (right - left) / 2 if arr[mid] == target { return mid
}else if arr[mid] < target { left = mid + 1 }else{ right = mid -1 } } return-1 }
PHP
function binarySearch(array $arr, int $target): int{ $left = 0;
$right = count($arr) -1;
while ($left <= $right) { $mid = $left + (int)(($right - $left) / 2);
if ($arr[$mid] === $target) return $mid;
if ($arr[$mid] < $target) $left = $mid +1;
else $right = $mid -1;
} return-1;
}
Perl
sub binarySearch { my ($arr_ref, $target) = @_;
my $left = 0;
my $right = $#{ $arr_ref };
while ($left <= $right) { my $mid = $left + int(($right - $left) / 2);
if ($arr_ref->[$mid] == $target) return $mid;
if ($arr_ref->[$mid] < $target) { $left = $mid +1; }
else { $right = $mid -1; }
} return-1;
}
Python
def binarySearch(arr, target):
left, right = 0, len(arr) -1 while left <= right:
mid = left + (right - left) // 2 if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1 else:
right = mid -1 return-1
Ruby
def binarySearch(arr, target)
left, right = 0, arr.length -1 while left <= right
mid = left + (right - left) / 2 return mid if arr[mid] == target
if arr[mid] < target
left = mid + 1 else right = mid -1 end end -1 end
Delphi
function BinarySearch(const Arr: arrayof Integer; Target: Integer): Integer;
var Left, Right, Mid: Integer;
begin Left := 0;
Right := High(Arr);
while Left <= Right do begin Mid := Left + (Right - Left) div2;
if Arr[Mid] = Target then Exit(Mid)
else if Arr[Mid] < Target then Left := Mid + 1 else Right := Mid -1;
end;
Result := -1;
end;
Rust
fn binarySearch(arr: &[i32], target: i32) -> Option<usize> {
letmut left = 0;
letmut right = arr.len();
while left < right {
let mid = left + (right - left) / 2;
match arr[mid].cmp(&target) {
std::cmp::Ordering::Equal => returnSome(mid),
std::cmp::Ordering::Less => left = mid + 1,
std::cmp::Ordering::Greater => right = mid,
}
}
None }
Assembler
mov eax, target
mov ebx, 0 mov ecx, n
dec ecx
search_loop: cmp ebx, ecx
jg not_found
mov edx, ebx
add edx, ecx
sub edx, ebx
shr edx, 1 add edx, ebx
mov esi, [arr + edx*4]
cmp esi, eax
je found
jl search_right
mov ecx, edx
dec ecx
jmp search_loop
search_right: mov ebx, edx
inc ebx
jmp search_loop
found: ; edx contains index
ret not_found: mov edx, -1 ret
JavaScript
function binarySearch(arr, target)
{ var left = 0;
var right = arr.length -1;
while (left <= right) { var mid = Math.floor(left + (right - left) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid +1;
else right = mid -1;
} return-1;
}
Lua
function binarySearch(arr, target)
local left, right = 1, #arr
while left <= right do local mid = math.floor(left + (right - left) / 2)
if arr[mid] == target then return mid
elseif arr[mid] < target then left = mid + 1 else right = mid -1 end end return-1 end