Esempio n. 1
0
function quickSort($arr)
{
    //先判断是否需要继续进行
    $length = count($arr);
    if ($length <= 1) {
        return $arr;
    }
    //选择第一个元素作为基准
    $base_num = $arr[0];
    //遍历除了标尺外的所有元素,按照大小关系放入两个数组内
    //初始化两个数组
    $left_array = array();
    //小于基准的
    $right_array = array();
    //大于基准的
    for ($i = 1; $i < $length; $i++) {
        if ($base_num > $arr[$i]) {
            //放入左边数组
            $left_array[] = $arr[$i];
        } else {
            //放入右边
            $right_array[] = $arr[$i];
        }
    }
    //再分别对左边和右边的数组进行相同的排序处理方式递归调用这个函数
    $left_array = quick_sort($left_array);
    $right_array = quick_sort($right_array);
    //合并
    return array_merge($left_array, array($base_num), $right_array);
}
Esempio n. 2
0
/**
*快速排序
*对$nums从$left到$right之间的数,进行从小到大的排序
*
*
*
*/
function quick_sort(&$nums, $left, $right)
{
    //如果$left<$right,那么排序才能正常进行,否则,已经不需要排序!
    if ($left < $right) {
        $x = $nums[$left];
        #选择一个基准
        $l = $left;
        $r = $right;
        while ($l < $r) {
            while ($l < $r && $nums[$r] > $x) {
                $r--;
            }
            if ($l < $r) {
                $nums[$l] = $nums[$r];
            }
            while ($l < $r && $nums[$l] <= $x) {
                $l++;
            }
            if ($l < $r) {
                $nums[$r] = $nums[$l];
            }
        }
        $nums[$l] = $x;
        quick_sort($nums, $left, $l - 1);
        quick_sort($nums, $l + 1, $right);
    }
}
Esempio n. 3
0
function quick_sort($arr)
{
    //先判断是否需要继续进行
    $length = count($arr);
    if ($length <= 1) {
        return $arr;
    }
    //如果没有返回,说明数组内的元素个数 多余1个,需要排序
    //选择一个标尺
    //选择第一个元素
    $base_num = $arr[0];
    //遍历 除了标尺外的所有元素,按照大小关系放入两个数组内
    //初始化两个数组
    $left_array = array();
    //小于标尺的
    $right_array = array();
    //大于标尺的
    for ($i = 1; $i < $length; $i++) {
        if ($base_num > $arr[$i]) {
            //放入左边数组
            $left_array[] = $arr[$i];
        } else {
            //放入右边
            $right_array[] = $arr[$i];
        }
    }
    //再分别对 左边 和 右边的数组进行相同的排序处理方式
    //递归调用这个函数,并记录结果
    $left_array = quick_sort($left_array);
    $right_array = quick_sort($right_array);
    //合并左边 标尺 右边
    return array_merge($left_array, array($base_num), $right_array);
}
Esempio n. 4
0
function quick_sort($arr)
{
    if (!is_array($arr)) {
        return $arr;
    }
    $len = count($arr);
    if ($len < 2) {
        return $arr;
    }
    $i = rand(1, $len - 1);
    $left = $right = array();
    for ($j = 0; $j < $len; $j++) {
        if ($j == $i) {
            continue;
        }
        if ($arr[$j] < $arr[$i]) {
            $left[] = $arr[$j];
        } else {
            $right[] = $arr[$j];
        }
    }
    $left = quick_sort($left);
    $right = quick_sort($right);
    $res = array_merge($left, array($arr[$i]), $right);
    return $res;
}
Esempio n. 5
0
function nox_binary($message, $dico, $t = 0)
{
    print "Binary Search\nDecryptage de " . $message . "\n";
    $handle = fopen($message, "r");
    $message_content = fread($handle, filesize($message));
    $handle = fopen($dico, "r");
    $dico_content = fread($handle, filesize($dico));
    $dico_arr = preg_split("/\n/", $dico_content);
    preg_match_all("/[^\\s\\d,;.!?\\-\\(\\)]+[-']?[^\\s\\d;,.?!\\-\\(\\)]+[-]?[^\\s\\d;,.?!\\-\\(\\)]+|[^\\s\\d;.?,!\\-\\(\\)]+/i", $message_content, $message_arr);
    $dico_arr = quick_sort($dico_arr);
    // shell_sort($dico_arr);
    $i = 0;
    $j = 0;
    $time_start = microtime(true);
    if ($t == 0) {
        foreach ($message_arr[0] as $word) {
            $j += binary_search_rec($dico_arr, $word, 0, count($dico_arr) - 1);
        }
    } else {
        foreach ($message_arr[0] as $word) {
            $j += binary_search_rec_trad($dico_arr, $word, 0, count($dico_arr) - 1);
        }
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    print "" . $j . " Mots du dictionnaire ont été trouvé dans le message.\n\n";
    print "Recherche terminée en " . round($time, 5) . " sec.\n";
}
Esempio n. 6
0
function get_message_array_and_display(&$sort_time_start, &$sort_time_end, &$time_end_all, &$result, &$time_start, &$time_end, &$time_start_display, &$time_end_display, $message_name, $list_dico)
{
    $handle = fopen($message_name, "r");
    $content = fread($handle, filesize($message_name));
    preg_match_all("/\\S+/", $content, $contents);
    $contents = $contents[0];
    $j = -1;
    $message = "\n" . '           -> ' . count($contents) . ' mots du dictionnaire chargés.' . "\n\n\n";
    echo $message;
    $time_start = microtime(true);
    foreach ($contents as $value) {
        if (isset($list_dico[$value])) {
            $result[++$j] = ' ♫ ' . $value;
        }
    }
    $time_end = microtime(true);
    $sort_time_start = microtime(true);
    $result = quick_sort($result);
    $sort_time_end = microtime(true);
    $counter = count($result);
    $i = -1;
    $j = 0;
    $time_start_display = microtime(true);
    echo '├─────║┤ Résultats ├║─────┤', "\n";
    for ($value = 0; isset($result[$value]); ++$value) {
        echo "\t", $result[$value], "\n";
    }
    $time_end_display = microtime(true);
    echo "\n";
    fclose($handle);
    return count($contents) . ' mots du dictionnaire chargés' . "\n" . $counter . ' mots du dictionnaire trouvés dans le message ' . "\n";
}
/**
 * @file quick_sort.php
 * @author lurenzhong@baidu.com
 * @date 16/2/23
 * @brief PHP快速排序算法
 * @version
 */
function quick_sort($arr)
{
    //判断是否需要排序,不需要直接返回原数组
    if (count($arr) <= 1) {
        return $arr;
    }
    //确定基准数
    $baseNum = $arr[0];
    $leftArr = array();
    $rightArr = array();
    for ($i = 1; $i < count($arr); $i++) {
        //第一个元素为基准数,不参与比较过程
        if ($arr[$i] < $baseNum) {
            $leftArr[] = $arr[$i];
        } else {
            $rightArr[] = $arr[$i];
        }
    }
    //这里会一直向下递归执行,直到$leftArr只有一个元素的时候,$rightArr也是。然后从最深处返回式地,依次执行下面的合并过程
    $leftArr = quick_sort($leftArr);
    $rightArr = quick_sort($rightArr);
    //用于调试理解递归调用过程,递归会一直往深处执行,直到$leftArr和$rightArr有返回结果时,执行下面的合并过程
    //printArr($leftArr);
    //printArr($rightArr);
    //echo '<br/>*************<br/>';
    $res = array_merge($leftArr, array($baseNum), $rightArr);
    //每次合并已排序好的$leftArr、$rightArr,则最终的结果也是已排序的
    return $res;
}
/**
 * Assuming $elements is array with numerical-index starting with zero, that's
 * $p, with upper bound index sizeof($elements) - 1, that's $r.
 * @param reference $elements Reference to array element.
 * @param int $p Starting index.
 * @param int $r Pivot index where each element $p to $r - 1 will be compared
 *               against.
 * @return void
 */
function quick_sort(&$elements, $p, $r, $fn = 'comparison_function')
{
    if ($p < $r) {
        $q = _partition($elements, $p, $r, $fn);
        quick_sort($elements, $p, $q - 1, $fn);
        quick_sort($elements, $q + 1, $r, $fn);
    }
}
Esempio n. 9
0
function quick_sort($arr)
{
    $count = count($arr);
    if ($count <= 1) {
        return $arr;
    }
    $left_arr = array();
    $right_arr = array();
    for ($index = 1; $index < $count; $index++) {
        if ($arr[0] >= $arr[$index]) {
            $left_arr[] = $arr[$index];
        } else {
            $right_arr[] = $arr[$index];
        }
    }
    return array_merge(quick_sort($left_arr), array($arr[0]), quick_sort($right_arr));
}
Esempio n. 10
0
function quick_sort($array)
{
    $len = count($array);
    if ($len < 2) {
        return $array;
    }
    $pivot = $array[0];
    $left = $right = array();
    for ($i = 1; $i < $len; $i++) {
        if ($array[$i] < $pivot) {
            $left[] = $array[$i];
        } else {
            $right[] = $array[$i];
        }
    }
    return array_merge(quick_sort($left), array($pivot), quick_sort($right));
}
Esempio n. 11
0
function quick_sort($arr)
{
    if (count($arr) < 2) {
        return $arr;
    }
    $key = $arr[0];
    $right_arr = $left_arr = array();
    for ($i = 1; $i < count($arr); $i++) {
        if ($arr[$i] < $key) {
            $right_arr[] = $arr[$i];
        } else {
            $left_arr[] = $arr[$i];
        }
    }
    $right_arr = quick_sort($right_arr);
    $left_arr = quick_sort($left_arr);
    return array_merge($right_arr, array($key), $left_arr);
}
function quick_sort($arr)
{
    $a_count = count($arr);
    if ($a_count <= 1) {
        return $arr;
    }
    $k = $arr[0];
    $left = array();
    $right = array();
    for ($i = 1; $i < $a_count; $i++) {
        if ($arr[$i] <= $k) {
            $left[] = $arr[$i];
        } else {
            $right[] = $arr[$i];
        }
    }
    $left = quick_sort($left);
    $right = quick_sort($right);
    return array_merge($left, array($k), $right);
}
Esempio n. 13
0
function quick_sort(array $data) {
	$count = count($data);
	if ($count <= 1) return $data;

	$key = $data[0];
	$left_arr = array();
	$right_arr = array();
	for ($i = 1; $i < $count; $i++) {
		if ($data[$i] <= $key) {
			$left_arr[] = $data[$i];
		} else {
			$right_arr[] = $data[$i];
		}

	}
	$left_arr = quick_sort($left_arr);
	$right_arr = quick_sort($right_arr);

	return array_merge($left_arr, array($key), $right_arr);
}
Esempio n. 14
0
function quick_sort($array)
{
    // find array size
    $length = count($array);
    // base case test, if array of length 0 then just return array to caller
    if ($length <= 1) {
        return $array;
    } else {
        // select an item to act as our pivot point, since list is unsorted first position is easiest
        $pivot = $array[0];
        // declare our two arrays to act as partitions
        $left = $right = array();
        // loop and compare each item in the array to the pivot value, place item in appropriate partition
        for ($i = 1; $i < count($array); $i++) {
            if ($array[$i] < $pivot) {
                $left[] = $array[$i];
            } else {
                $right[] = $array[$i];
            }
        }
        // use recursion to now sort the left and right lists
        return array_merge(quick_sort($left), array($pivot), quick_sort($right));
    }
}
/**
 * 快速排序
 */
function quick_sort($arr)
{
    $len = count($arr);
    if ($len <= 1) {
        return $arr;
    }
    $leftArr = $rightArr = array();
    $key = $arr[0];
    for ($i = 1; $i < $len; $i++) {
        if ($arr[$i] <= $key) {
            $leftArr[] = $arr[$i];
        } else {
            $rightArr[] = $arr[$i];
        }
    }
    $leftArr = quick_sort($leftArr);
    $rightArr = quick_sort($rightArr);
    $rs = array_merge($leftArr, array($key), $rightArr);
    return $rs;
}