Last active
February 6, 2016 11:10
-
-
Save stoimen/02c44ea1d8238d5f39dc to your computer and use it in GitHub Desktop.
Sequential search
This file contains hidden or 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 | |
// unordered list | |
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8); | |
// searched value | |
$x = 3.14; | |
$index = count($arr); | |
while ($arr[$index--] != $x); | |
echo "The value $x found on position " . ($index+1) . "!"; | |
?> |
This file contains hidden or 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 | |
// unordered list | |
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8); | |
// searche value | |
$x = 3.14; | |
$arr[] = $x; | |
$index = 0; | |
while ($arr[$index++] != $x); | |
if ($index < count($arr)) { | |
echo "The value $x found on position " . ($index - 1) . "!"; | |
} else { | |
echo "The value $x not found!"; | |
} | |
?> |
This file contains hidden or 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 | |
// unordered list | |
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8); | |
// searched value | |
$x = 3.14; | |
$index = null; | |
for ($i = 0; $i < count($arr); $i++) { | |
if ($arr[$i] == $x) { | |
$index = $i; | |
} | |
} | |
if (isset($index)) { | |
return true; | |
} else { | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment