Beispiel #1
0
/**
 * 快速排序
 * C.R.A.Hoare于1962年提出的一种划分交换排序。
 * 它采用了一种分而治之的策略,
 * 通常称其为分治法(Divide-and-ConquerMethod)。
 * 基本思想:
 * 将原问题分解为若干个规模更小但结构与原问题相似的子问题。
 * 递归地解这些子问题,然后将这些子问题的解组合为原问题的解。
 *
 */
function quickSort(array &$list, $left = 0, $right = null)
{
    if (is_null($right)) {
        $right = count($list) - 1;
    }
    $middle = intval(($left + $right) / 2);
    $pivot = $list[$middle];
    $i = $left;
    $j = $right;
    //partition
    while ($i <= $j) {
        while ($list[$i] < $pivot) {
            $i++;
        }
        while ($list[$j] > $pivot) {
            $j--;
        }
        if ($i <= $j) {
            $temp = $list[$i];
            $list[$i] = $list[$j];
            $list[$j] = $temp;
            $i++;
            $j--;
        }
    }
    //recusion
    if ($left < $j) {
        quickSort($list, $left, $j);
    }
    if ($i < $right) {
        quickSort($list, $i, $right);
    }
}
Beispiel #2
0
function quickSort($arr)
{
    if(count($arr)==1) return $arr;
    $pivot=array_pop($arr);
    $lower=array();
    $upper=array();
    foreach($arr as $val)
    {
        if($val>$pivot)
        {
            array_push($upper,$val);
        }
        else
        {
            array_push($lower,$val);
        }
    }
    
    if(count($lower)==0)
    {
        $ret=$lower;
    }
    else
    {
        $ret=quickSort($lower);
    }
    array_push($ret,$pivot);
    if(count($upper)!=0)
    {
        $ret=array_merge($ret,quickSort($upper));
    }
    return $ret;
}
Beispiel #3
0
function findSerialBiggerElement($array, $serial)
{
    $left = 0;
    $right = count($array) - 1;
    quickSort($array, $left, $right);
    return $array[$right - $serial + 1];
}
Beispiel #4
0
function quickSort(array &$a, $l, $r)
{
    if ($l < $r) {
        $i = $l;
        $j = $r;
        $x = $a[$i];
        while ($i < $j) {
            while ($i < $j && $a[$j] > $x) {
                $j--;
            }
            if ($i < $j) {
                $a[$i++] = $a[$j];
            }
            while ($i < $j && $a[$i] < $x) {
                $i++;
            }
            if ($i < $j) {
                $a[$j++] = $a[$i];
            }
        }
        $a[$i] = $x;
        quickSort($a, $l, $i - 1);
        quickSort($a, $i + 1, $r);
    }
}
function quickSort(&$arr, $leftIndex, $rightIndex)
{
    $index = partition($arr, $leftIndex, $rightIndex);
    if ($leftIndex < $index - 1) {
        quickSort($arr, $leftIndex, $index - 1);
    }
    if ($index < $rightIndex) {
        quickSort($arr, $index, $rightIndex);
    }
}
Beispiel #6
0
function quickSort(array &$arr, $startIndex = 0, $stopIndex = null)
{
    $stopIndex = $stopIndex !== null ? $stopIndex : count($arr) - 1;
    if ($stopIndex - $startIndex <= 1) {
        return $arr;
    }
    $pivotIndex = choosePivot($arr, $startIndex, $stopIndex);
    $pivotIndex = partition($arr, $pivotIndex, $startIndex, $stopIndex);
    quickSort($arr, 0, $pivotIndex - 1);
    quickSort($arr, $pivotIndex + 1, $stopIndex);
}
Beispiel #7
0
function quickSort($array)
{
    $length = count($array);
    if ($length <= 1) {
        return $array;
    } else {
        $start = $array[0];
        $lhs = $rhs = array();
        for ($i = 1; $i < count($array); $i++) {
            if ($array[$i] < $start) {
                $lhs[] = $array[$i];
            } else {
                $rhs[] = $array[$i];
            }
        }
        return array_merge(quickSort($lhs), array($start), quickSort($rhs));
    }
}
function quickSort(array $arr)
{
    $count = count($arr);
    if ($count <= 1) {
        return $arr;
    }
    $first_val = $arr[0];
    $left_arr = array();
    $right_arr = array();
    for ($i = 1; $i < $count; $i++) {
        if ($arr[$i] <= $first_val) {
            $left_arr[] = $arr[$i];
        } else {
            $right_arr[] = $arr[$i];
        }
    }
    $left_arr = quickSort($left_arr);
    $right_arr = quickSort($right_arr);
    return array_merge($left_arr, array($first_val), $right_arr);
}
Beispiel #9
0
function quickSort($quick)
{
    $len = count($quick);
    if ($len <= 1) {
        return $quick;
    }
    $base = $quick[0];
    $left = array();
    $right = array();
    for ($i = 1; $i < $len; $i++) {
        if ($quick[$i] > $base) {
            $right[] = $quick[$i];
        } else {
            $left[] = $quick[$i];
        }
    }
    $left = quickSort($left);
    $right = quickSort($right);
    echo implode("\t", array_merge($left, array($base), $right)) . "\n";
    //打印排序过程
    return array_merge($left, array($base), $right);
}
Beispiel #10
0
function quickSort(&$list, $first, $last)
{
    $firstPointer = $first;
    $lastPointer = $last;
    //枢軸値を決める。配列の中央値
    $centerValue = $list[intVal(($firstPointer + $lastPointer) / 2)];
    //並び替えができなくなるまで
    do {
        //枢軸よりも左側で値が小さい場合はポインターは進める
        while ($list[$firstPointer] < $centerValue) {
            $firstPointer++;
        }
        //枢軸よりも右側で値が大きい場合はポインターを減らす
        while ($list[$lastPointer] > $centerValue) {
            $lastPointer--;
        }
        //この操作で左側と右側の値を交換する場所は特定
        if ($firstPointer <= $lastPointer) {
            //ポインターが逆転していない時は交換可能
            $tmp = $list[$lastPointer];
            $list[$lastPointer] = $list[$firstPointer];
            $list[$firstPointer] = $tmp;
            //ポインタを進めて分割する位置を指定
            $firstPointer++;
            $lastPointer--;
        }
    } while ($firstPointer <= $lastPointer);
    if ($first < $lastPointer) {
        //左側が比較可能の時
        quickSort($list, $first, $lastPointer);
    }
    if ($firstPointer < $last) {
        //右側が比較可能時
        quickSort($list, $firstPointer, $last);
    }
}
Beispiel #11
0
function _quickSort($array, $field)
{
    $count = count($array);
    if ($count <= 1) {
        return $array;
    }
    $key = $array[0];
    $left_array = array();
    $middle_array = array();
    $right_array = array();
    foreach ($array as $k => $val) {
        //这里改变大于小于,改变数组的排序
        //如if ($key[$field] > $val[$field]) {
        if ($key[$field] > $val[$field]) {
            $left_array[] = $val;
        } else {
            if ($key[$field] == $val[$field]) {
                $middle_array[] = $val;
                //直接插入
            } else {
                $right_array[] = $val;
            }
        }
    }
    //递归
    $left_array = quickSort($left_array, $field);
    $right_array = quickSort($right_array, $field);
    //合并数组
    $array = array_merge($left_array, $middle_array, $right_array);
    return $array;
}
Beispiel #12
0
        $invalidErr = true;
    } else {
        $labelM = "";
        $invalidErr = false;
        $numArrs = explode(" ", $numVal);
        switch ($sortType) {
            case 0:
                $sortedArrs = bubbleSort($numArrs);
                $labelM = "sorted using bubble sort <br> RESULT:";
                break;
            case 1:
                $sortedArrs = selectionSort($numArrs);
                $labelM = "sorted using select sort <br> RESULT:";
                break;
            case 2:
                $sortedArrs = quickSort($numArrs);
                $labelM = "sorted using quick sort <br> RESULT:";
                break;
            default:
                sort($sortedArrs);
                $labelM = "sorted using default php sort <br> RESULT:";
        }
    }
}
function quickSort($arr)
{
    $loe = $gt = array();
    if (count($arr) < 2) {
        return $arr;
    }
    $p_key = key($arr);
Beispiel #13
0
/**
 * Commonly used, O(n log n) average, worst case O(n2) rare
 * Couldn't get this working in place when I tried, will have to return
 */
function quickSort(array $myArray)
{
    if (count($myArray) < 2) {
        return $myArray;
    }
    $p = ceil(count($myArray) / 2);
    //we'll put it in the middle
    $pivot = $myArray[$p];
    unset($myArray[$p]);
    $lo = $hi = [];
    foreach ($myArray as $val) {
        if ($val > $pivot) {
            $hi[] = $val;
        } else {
            $lo[] = $val;
        }
    }
    $first = quickSort($lo);
    $last = quickSort($hi);
    return array_merge($first, [$pivot], $last);
}
Beispiel #14
0
/**
 * 快速排序算法。
 * @param $Array
 * @return 排序后的Array。
 **/
function quickSort($array)
{
    if (count($array) <= 1) {
        return $array;
    }
    $key = $array[0];
    $left_arr = array();
    $right_arr = array();
    for ($i = 1; $i < count($array); $i++) {
        if ($array[$i] <= $key) {
            $left_arr[] = $array[$i];
        } else {
            $right_arr[] = $array[$i];
        }
    }
    $left_arr = quickSort($left_arr);
    $right_arr = quickSort($right_arr);
    return array_merge($left_arr, array($key), $right_arr);
}
Beispiel #15
0
            if ($vector[$left] < $vector[$left - 1]) {
                $aux = $vector[$left];
                $vector[$left] = $vector[$left - 1];
                $vector[$left - 1] = $aux;
            }
            if ($vector[$right] > $vector[$right + 1]) {
                $aux = $vector[$right];
                $vector[$right] = $vector[$right + 1];
                $vector[$right + 1] = $aux;
            }
            --$left;
            ++$right;
            printv($vector);
        }
        ++$ul;
        --$ur;
    }
}
function quickSort($vector)
{
    $pivot = $vector[0];
    $length = intval(count($vector) / 2);
    $aux = $vector[0];
    $vector[0] = $vector[$length];
    $vector[$length] = $aux;
    printv($vector);
}
//bubble_step([7, 2 ,9, 4, 3, 3, 8, 6, 1, 10]);
//bubble_bidirectional([7, 2 ,9, 4, 3, 8, 6, 10, 1]);
quickSort([7, 2, 9, 4, 3, 8, 6, 10, 1, 12]);
Beispiel #16
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 = quickSort($left_array);
    $right_array = quickSort($right_array);
    //合并
    return array_merge($left_array, array($base_num), $right_array);
}
Beispiel #17
0
function PHPMin($a_Data)
{
    $i = '';
    $j = '';
    $Bound = '';
    $temp = '';
    $a_Data = quickSort($a_Data);
    $Bound = uBound($a_Data);
    for ($i = 0; $i <= $Bound; $i++) {
        for ($j = $i + 1; $j <= $Bound; $j++) {
            if ($a_Data[$j] > $a_Data[$i]) {
                $temp = $a_Data[$i];
                $a_Data[$i] = $a_Data[$j];
                $a_Data[$j] = $temp;
            }
        }
    }
    $PHPMin = $a_Data[$Bound];
    return @$PHPMin;
}
Beispiel #18
0
/**
 * 快速排序
 */
function quickSort($arr)
{
    $len = count($arr);
    if ($len > 1) {
        $mid = $arr[0];
        $left_arr = $right_arr = array();
        for ($i = 1; $i < $len; $i++) {
            if ($arr[$i] <= $mid) {
                $left_arr[] = $arr[$i];
            } else {
                $right_arr[] = $arr[$i];
            }
        }
        $left_arr = quickSort($left_arr);
        $right_arr = quickSort($right_arr);
        $arr = array_merge($left_arr, array($mid), $right_arr);
    }
    return $arr;
}
Beispiel #19
0
function custom_term_sort($terms, $taxonomies, $args)
{
    // Controls behavior when get_terms is called at unusual times resulting in a terms array without objects
    $empty = false;
    // Create collector arrays
    $ordered_terms = array();
    $unordered_terms = array();
    // Add taxonomy order to terms
    foreach ($terms as $term) {
        // Only set tax_order if value is an object
        if (is_object($term)) {
            $term_meta = get_option("taxonomy_{$term->term_id}");
            if ($taxonomy_sort = $term_meta['order_meta']) {
                $term->tax_order = (int) $taxonomy_sort;
                $ordered_terms[] = $term;
            } else {
                $term->tax_order = (int) 0;
                $unordered_terms[] = $term;
            }
        } else {
            $empty = true;
        }
    }
    // Only sort by tax_order if there are items to sort, otherwise return the original array
    if (!$empty && count($ordered_terms) > 0) {
        quickSort($ordered_terms);
    } else {
        return $terms;
    }
    // Combine the newly ordered items with the unordered items and return
    return array_merge($ordered_terms, $unordered_terms);
}