Exemplo n.º 1
0
/**
 * @param array $array
 * @return array
 */
function merge_sort(array $array)
{
    $size = sizeof($array);
    if ($size > 1) {
        $pivot = floor($size / 2);
        $left = merge_sort(array_slice($array, 0, $pivot));
        $right = merge_sort(array_slice($array, $pivot, $size));
        $counter1 = $counter2 = 0;
        for ($i = 0; $i < $size; $i++) {
            // if we're done processing one half, take the rest from the 2nd half
            if ($counter1 == sizeof($left)) {
                $array[$i] = $right[$counter2];
                ++$counter2;
                // if we're done with the 2nd half as well or as long as pieces
                // are still smaller than the 2nd halfin the first half
            } elseif ($counter2 == sizeof($right) or $left[$counter1] < $right[$counter2]) {
                $array[$i] = $left[$counter1];
                ++$counter1;
            } else {
                $array[$i] = $right[$counter2];
                ++$counter2;
            }
        }
    }
    return $array;
}
/**
 * This is the main function to be called if you need to sort array using
 * merget sort algorithm. This function recursively divide the $elements into
 * two portions, half and right, until one partition contains single element.
 * Then it bubble up from bottom to top by merging smaller partition
 * that already sorted into bigger partition. Initial call to this function
 * should be merge_sort($elements, 1, sizeof($elements)).
 * @param reference $elements Reference to array element.
 * @param int $p Lower bound index of $elements.
 * @param int $r Upper bound index of $elements.
 * @param string $fn Function used as a comparison function.
 * @returns void
 */
function merge_sort(&$elements, $p, $r, $fn = 'comparison_function')
{
    if ($p < $r) {
        $q = floor(($p + $r) / 2);
        merge_sort($elements, $p, $q);
        // left partition
        merge_sort($elements, $q + 1, $r);
        // right partition
        _merge($elements, $p, $q, $r, $fn);
        // sort the given elements partition
    }
}
Exemplo n.º 3
0
function merge_sort($arr)
{
    if (count($arr) <= 1) {
        return $arr;
    }
    $left = array_slice($arr, 0, (int) (count($arr) / 2));
    $right = array_slice($arr, (int) (count($arr) / 2));
    $left = merge_sort($left);
    $right = merge_sort($right);
    $output = merge($left, $right);
    return $output;
}
function merge_sort(&$a, $l = 0, $r = -1, $tmp_v = array())
{
    // gestisce la prima chiamata, quella senza argomenti
    if ($r == -1) {
        $r = count($a) - 1;
        $tmp_v = $a;
    }
    // Distingue i casi elementari (1 o 2 elementi)
    if ($l == $r) {
        $pa = array_slice($a, $l, $r - $l + 1);
        print_array($pa, "Dividi - caso elementare 1 elemento", "({$l} {$r})");
    } elseif ($r == $l + 1) {
        $pa = array_slice($a, $l, $r - $l + 1);
        print_array($pa, "Dividi - caso elementare 2 elementi", "({$l} {$r})");
        if ($a[$l] > $a[$r]) {
            $tmp = $a[$r];
            $a[$r] = $a[$l];
            $a[$l] = $tmp;
        }
        $pa = array_slice($a, $l, $r - $l + 1);
        print_array($pa, "Ordina - caso elementare 2 elementi", "({$l} {$r})");
    } elseif ($l != $r) {
        // Caso non elementare
        $pa = array_slice($a, $l, $r - $l + 1);
        print_array($pa, "Dividi - caso non elementare elementi", "({$l} {$r})");
        $center = round(($l + $r) / 2, 0, PHP_ROUND_HALF_DOWN);
        merge_sort($a, $l, $center);
        merge_sort($a, $center + 1, $r);
    }
    // merge
    $n = $r - $l + 1;
    $c = round($n / 2, 0);
    for ($i = 0; $i < $n; $i++) {
        $tmp_v[$i] = $a[$l + $i];
    }
    $i = $j = 0;
    while ($i < $c && $j < $n - $c) {
        if ($tmp_v[$i] < $tmp_v[$c + $j]) {
            $a[$l + $i + $j] = $tmp_v[$i];
            $i++;
        } else {
            $a[$l + $i + $j] = $tmp_v[$c + $j];
            $j++;
        }
    }
    while ($i < $c) {
        $a[$l + $i + $j] = $tmp_v[$i];
        $i++;
    }
    $pa = array_slice($a, $l, $r - $l + 1);
    print_array($pa, "Fondi ", "({$l} {$r})");
}
Exemplo n.º 5
0
function merge_sort($array)
{
    if (count($array) == 1) {
        return $array;
    }
    $middle = (int) (count($array) / 2);
    $left = array_slice($array, 0, $middle);
    $right = array_slice($array, $middle);
    $left = merge_sort($left);
    $right = merge_sort($right);
    $return_array = merge($left, $right);
    return $return_array;
}
 /**
  * 栏目信息
  * @param  int    $site_id    要查询的网站id
  * @param  bool   $forbidden  是否在乎forbidden
  * @return bool|array     false 查询失败    array 查询结果
  */
 public function get_column_info($site_id, $forbidden = false)
 {
     $where['a.site_id'] = $site_id;
     $where['a.is_default'] = 1;
     if ($forbidden) {
         $where['a.forbidden'] = 0;
     }
     $admin_result = $this->alias('a')->field('a.id, a.name, a.forbidden, a.sort, a.url, b.savepath, b.savename')->join('left join picture as b on a.icon = b.id')->where($where)->order('sort')->select();
     $where['a.is_default'] = 0;
     $home_result = $this->alias('a')->field('a.id, a.name, a.forbidden, a.sort, a.url, b.savepath, b.savename')->join('left join home_picture as b on a.icon = b.id')->where($where)->order('sort')->select();
     $result = merge_sort($admin_result, $home_result);
     if (!$result) {
         return false;
     }
     $root = C('UPLOAD_ROOT');
     foreach ($result as $key => $value) {
         $result[$key]['icon_url'] = $root . $value['savepath'] . $value['savename'];
     }
     //echo $this->getLastSql();
     return $result;
 }
Exemplo n.º 7
0
/**
 * merge_sort: 
 * 
 * @link   https://en.wikipedia.org/wiki/Merge_sort
 *
 * @param  array $list The unordered list of numbers
 *
 * @return array       The ordered list of numbers
 */
function merge_sort(array $list)
{
    if (1 === count($list)) {
        return $list;
    }
    $left = merge_sort(array_slice($list, 0, floor(count($list) / 2)));
    $right = merge_sort(array_slice($list, floor(count($list) / 2)));
    $merged = array();
    while (count($left) > 0 or count($right) > 0) {
        if (count($left) > 0 and count($right) > 0) {
            if ($left[0] <= $right[0]) {
                $merged[] = array_shift($left);
            } else {
                $merged[] = array_shift($right);
            }
        } elseif (count($left) > 0) {
            $merged[] = array_shift($left);
        } elseif (count($right) > 0) {
            $merged[] = array_shift($right);
        }
    }
    return $merged;
}
Exemplo n.º 8
0
function merge_sort($a, $deep = 0){
	global $swaps;

	if (count($a) <= 1)
		return $a;

	// Split by 2
	$mid = (int) (count($a) / 2);

	// sort halves
	$a1 = merge_sort(array_slice($a, 0, $mid	), $deep + 1);
	$a2 = merge_sort(array_slice($a, $mid		), $deep + 1);

	// merge the partitions
	$aa = array();

	$v1 = array_shift($a1);
	$v2 = array_shift($a2);

	while($v1 || $v2){
		if ($v1)
			if ($v1 <= $v2 || $v2 === null){
				$aa[] = $v1;
				$v1 = array_shift($a1);
				$swaps++;
			}

		if ($v2)
			if ($v2 < $v1 || $v1 === null){
				$aa[] = $v2;
				$v2 = array_shift($a2);
				$swaps++;
			}
	}

	return $aa;
}
Exemplo n.º 9
0
 protected static function getTable($key)
 {
     if (isset(self::$_cachedTables[$key])) {
         return self::$_cachedTables[key];
     }
     $int32Max = pow(2, 32);
     $table = array();
     $decrypt_table = array();
     $hash = md5($key, true);
     $tmp = unpack('V2', $hash);
     $al = $tmp[1];
     $ah = $tmp[2];
     $i = 0;
     while ($i < 256) {
         $table[$i] = $i;
         $i++;
     }
     $i = 1;
     while ($i < 1024) {
         $table = merge_sort($table, function ($x, $y) use($ah, $al, $i, $int32Max) {
             return ($ah % ($x + $i) * $int32Max + $al) % ($x + $i) - ($ah % ($y + $i) * $int32Max + $al) % ($y + $i);
         });
         $i++;
     }
     $table = array_values($table);
     $i = 0;
     while ($i < 256) {
         $decrypt_table[$table[$i]] = $i;
         ++$i;
     }
     ksort($decrypt_table);
     $decrypt_table = array_values($decrypt_table);
     $result = array($table, $decrypt_table);
     self::$_cachedTables[$key] = $result;
     return $result;
 }
Exemplo n.º 10
0
/**
 * Array sorting function
 * Use merge algorythm for sorting
 *
 * @param array $a Array for sorting
 * @return array $new_arr Sorting array
 */
function merge_sort(array $a)
{
    if (count($a) <= 1) {
        return $a;
    }
    $arr1 = array_slice($a, 0, count($a) / 2);
    $arr2 = array_slice($a, count($a) / 2);
    $arr1 = merge_sort($arr1);
    $arr2 = merge_sort($arr2);
    do {
        if ($arr1[0] < $arr2[0]) {
            $new_arr[] = array_shift($arr1);
        } else {
            $new_arr[] = array_shift($arr2);
        }
        if (count($arr1) == 0) {
            $new_arr = array_merge($new_arr, $arr2);
        }
        if (count($arr2) == 0) {
            $new_arr = array_merge($new_arr, $arr1);
        }
    } while (count($arr1) && count($arr2));
    return $new_arr;
}
function merge_sort(&$arr)
{
    $a_count = count($arr);
    if ($a_count <= 1) {
        return $arr;
    }
    $m = intval($a_count / 2);
    $arr1 = array_slice($arr, 0, $m);
    $arr2 = array_slice($arr, $m);
    merge_sort($arr1);
    merge_sort($arr2);
    merge($arr1, $arr2, $arr);
    return $arr;
}
Exemplo n.º 12
0
            $i++;
        }
    }
    if ($left_put) {
        for (; $i < count($left); $i++) {
            array_push($ra, $left[$i]);
        }
    }
    if ($right_put) {
        for (; $j < count($right); $j++) {
            array_push($ra, $right[$j]);
        }
    }
    return $ra;
}
r_print(merge_sort($array_to_sort));
///////////////
//Quick Sort//
///////////////
function qs($a, $low, $high)
{
    if ($low == $high || $low > $high) {
        return $a;
    } else {
        $flag1 = false;
        $flag2 = false;
        $pivot = $a[$low];
        for ($i = $low + 1, $j = $high;;) {
            if ($j < $i) {
                break;
            }