Skip to content

Instantly share code, notes, and snippets.

@evrekhman
Last active September 28, 2018 15:18
Show Gist options
  • Save evrekhman/750a33833769a5dd050db509e0a530cc to your computer and use it in GitHub Desktop.
Save evrekhman/750a33833769a5dd050db509e0a530cc to your computer and use it in GitHub Desktop.
1\tзначение1\x0A2\tзначение2\x0A3\tзначение3\x0A4\tзначение4\x0A5\tзначение5\x0A6\tзначение6\x0A7\tзначение7\x0A8\tзначение8\x0A9\tзначение9\x0A10\tзначение10
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
function binarySearch($fileName , $x){
$handle = fopen("bin.txt", "rb");
$fread = fread($handle,filesize("bin.txt"));
$explode1 = explode("\\x0A",$fread);
$a = array();
foreach($explode1 as $key=>$value){
$ex2 = explode("\\t",$value);
$a[$ex2[0]]=$ex2[1];
}
if (count($a) === 0) return false;
$low = 0;
$high = count($a);
while ($low <= $high) {
// compute middle index
$mid = floor(($low + $high) / 2);
if($a[$low] == $x) {
return true;
echo $a[$mid];
}
if ($x < $a[$mid]) {
// search the left side of the array
$high = $mid -1;
echo $high;
}
else {
// search the right side of the array
$low = $mid + 1;
echo $low;
}
}
// If we reach here element x doesnt exist
return false;
}
$val = "8";
if(binarySearch("bin", $val) == true) {
//echo $val." Exists";
}
else {
//echo $val." Doesnt Exist";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment