Skip to content

Instantly share code, notes, and snippets.

@stoimen
stoimen / sequential_search_reverse.php
Last active February 6, 2016 11:10
Sequential search
<?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);
@stoimen
stoimen / sequential_search_v2.php
Last active August 29, 2015 14:10
Sequential Search v2
<?php
// unordered list
$arr = array(1, 2, 3, 3.14, 5, 4, 6, 9, 8);
// searched value
$x = 3.14;
$length = count($arr);
$index = null;
for ($i = 0; $i < $length; $i++) {
@stoimen
stoimen / sequential_search.js
Last active August 29, 2015 14:10
Sequential Search (js)
(function() {
var sequential = function(haystack, needle) {
var i = 0,
len = haystack.length
;
for (; i < len; i++) {
if (haystack[i] === needle) {
return true;
}
@stoimen
stoimen / linear_search.php
Last active August 29, 2015 14:10
Linear Search in Sorted Lists
<?php
/**
* Performs a sequential search using sentinel
* and changes the array after the value is found
*
* @param array $arr
* @param mixed $value
*/
function sequential_search(&$arr, $value)
{
@stoimen
stoimen / eratosthenes_sieve.php
Last active March 26, 2017 09:37
Determine if a Number is Prime
<?php
function eratosthenes_sieve(&$sieve, $n) {
$i = 2;
while ($i <= $n) {
if ($sieve[$i] == 0) {
echo $i;
$j = $i;
while ($j <= $n) {
@stoimen
stoimen / uniq.js
Created May 4, 2015 13:16
JavaScript Array Unique
array.filter(function(elem, index, self) {
return self.indexOf(elem) === index;
});
<?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++) {
(function(root) {
function sequential(list, item) {
for (var i = 0, l = list.length; i < l; i++) {
if (list[i] == item) {
return i;
}
}
return -1;
var sequential = function(list, item) {
for (var i = 0, l = list.length; i < l; i++) {
if (list[i] == item) {
return i;
}
}
return -1;
};
<?php
$list = array(3,4,2,5,6,7,8,2,5,1,4,4,6);
function minimum($list)
{
$len = count($list);
$minimum = $list[0];
for ($i = 1; $i < $len; $i++) {
if ($minimum > $list[$i]) {