コード例 #1
0
ファイル: test0.php プロジェクト: robstoll/tins-benchmark
function shellsort(array $arr)
{
    $n = myCount($arr);
    $t = myCeil(myLog($n, 2));
    $d = [0, 1];
    for ($i = 2; $i <= $t; $i++) {
        $d[$i] = 2 * $d[$i - 1] + 1;
    }
    $d = myArrayReverse($d);
    foreach ($d as $curIncrement) {
        for ($i = $curIncrement; $i < $n; $i++) {
            $x = $arr[$i];
            $j = $i - $curIncrement;
            while ($j >= 0 && $x < $arr[$j]) {
                $arr[$j + $curIncrement] = $arr[$j];
                $j = $j - $curIncrement;
            }
            $arr[$j + $curIncrement] = $x;
        }
    }
    return $arr;
}
コード例 #2
0
ファイル: test2.php プロジェクト: robstoll/tins-benchmark
function a_star($start, $target, $map)
{
    $open_heap = array($start);
    // binary min-heap of indexes with values in $f
    $open = array($start => TRUE);
    // set of indexes
    $closed = array();
    // set of indexes
    $g = [];
    $h = [];
    $f = [];
    $from = [];
    $g[$start] = 0;
    $h[$start] = heuristic($start, $target);
    $f[$start] = $g[$start] + $h[$start];
    while ($open) {
        $i = heap_pop($open_heap, $f);
        //not yet supported
        //unset($open[$i]);
        $open[$i] = null;
        $closed[$i] = TRUE;
        if ($i == $target) {
            $path = array();
            for (; $i != $start; $i = $from[$i]) {
                $path[myCount($path)] = $i;
            }
            return myArrayReverse($path);
        }
        foreach (neighbors($i, $map) as $j => $step) {
            if (!myArrayKeyExists($j, $closed)) {
                if (!myArrayKeyExists($j, $open) || $g[$i] + $step < $g[$j]) {
                    $g[$j] = $g[$i] + $step;
                    $h[$j] = heuristic($j, $target);
                    $f[$j] = $g[$j] + $h[$j];
                    $from[$j] = $i;
                    if (!myArrayKeyExists($j, $open)) {
                        $open[$j] = TRUE;
                        heap_push($open_heap, $f, $j);
                    } else {
                        heap_raise($open_heap, $f, $j);
                    }
                }
            }
        }
    }
    return FALSE;
}