Last active
January 6, 2024 14:28
-
-
Save Krasipeace/4761fc0863fdb2a13c735bba39d374b7 to your computer and use it in GitHub Desktop.
Primitive Binary Search (cs, js, php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int BinarySearchIterative(int[] array, int target) | |
{ | |
int left = 0; | |
int right = array.Length - 1; | |
while (left <= right) | |
{ | |
int mid = left + (right - left) / 2; | |
if (array[mid] == target) | |
{ | |
return mid; | |
} | |
else if (array[mid] < target) | |
{ | |
left = mid + 1; | |
} | |
else | |
{ | |
right = mid - 1; | |
} | |
} | |
return -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binarySearchIterative(array, target) { | |
let left = 0; | |
let right = array.length - 1; | |
while (left <= right) { | |
let mid = Math.floor(left + (right - left) / 2); | |
if (array[mid] === target) { | |
return mid; | |
} else if (array[mid] < target) { | |
left = mid + 1; | |
} else { | |
right = mid - 1; | |
} | |
} | |
return -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function binarySearchIterative($array, $target) { | |
$left = 0; | |
$right = count($array) - 1; | |
while ($left <= $right) { | |
$mid = floor($left + ($right - $left) / 2); | |
if ($array[$mid] == $target) { | |
return $mid; | |
} else if ($array[$mid] < $target) { | |
$left = $mid + 1; | |
} else { | |
$right = $mid - 1; | |
} | |
} | |
return -1; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int BinarySearchRecursive(int[] array, int target, int left, int right) | |
{ | |
if (right >= left) | |
{ | |
int mid = left + (right - left) / 2; | |
if (array[mid] == target) | |
{ | |
return mid; | |
} | |
else if (array[mid] > target) | |
{ | |
return BinarySearchRecursive(array, target, left, mid - 1); | |
} | |
else | |
{ | |
return BinarySearchRecursive(array, target, mid + 1, right); | |
} | |
} | |
return -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function binarySearchRecursive(array, target, left, right) { | |
if (right >= left) { | |
let mid = Math.floor(left + (right - left) / 2); | |
if (array[mid] === target) { | |
return mid; | |
} else if (array[mid] > target) { | |
return binarySearchRecursive(array, target, left, mid - 1); | |
} else { | |
return binarySearchRecursive(array, target, mid + 1, right); | |
} | |
} | |
return -1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function binarySearchRecursive($array, $target, $left, $right) { | |
if ($right >= $left) { | |
$mid = floor($left + ($right - $left) / 2); | |
if ($array[$mid] == $target) { | |
return $mid; | |
} else if ($array[$mid] > $target) { | |
return binarySearchRecursive($array, $target, $left, $mid - 1); | |
} else { | |
return binarySearchRecursive($array, $target, $mid + 1, $right); | |
} | |
} | |
return -1; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment