PHP基础排序算法(四)快速排序

来源:互联网 发布:directx11编程 编辑:程序博客网 时间:2024/06/04 19:02

PHP基础排序算法之快速排序

<?php/** * @快速排序 * @排序思路: *       和冒泡排序算法类似,都是基于交换排序思想。 * @流程: *      (1)首先设定一个分界值,通过该分界值将数组分为左右两部分。 *      (2)将大于等于分界值的数据集中到数组的右边,小于分界值的数据集中到数组的左边。 *      (3)然后,左右两边的数据可以独立排序,分别重复(1)(2)步骤 *      (4)可以看出,这是一个递归排序。当左、右两部分都排序好了,便可按照从小到大的顺序排好。 *//** * @方案一 * @desc 快速排序算法ASC * @author lxs */function quickSortAscOne($arr) {    $count = count($arr);    if ($count <= 1) {        return $arr;    }    //选择一个基准位置    $index = floor($count/2);    $arr_left = array();    $arr_right = array();    for($x = 0; $x < $count; $x++) {        if ($index != $x) {            if ($arr[$x] < $arr[$index]) {                $arr_left[] = $arr[$x];            }            else {                $arr_right[] = $arr[$x];            }        }    }    $arr_left = quickSortAscOne($arr_left);    $arr_right = quickSortAscOne($arr_right);    return array_merge($arr_left, array($arr[$index]), $arr_right);}/** * @方案一 * @desc 快速排序算法DESC * @author lxs */function quickSortDescOne($arr) {    $count = count($arr);    if ($count <= 1) {        return $arr;    }    //选择一个基准位置    $index = floor($count/2);    $arr_left = array();    $arr_right = array();    for($x = 0; $x < $count; $x++) {        if ($index != $x) {            if ($arr[$x] >= $arr[$index]) {                $arr_left[] = $arr[$x];            }            else {                $arr_right[] = $arr[$x];            }        }    }    $arr_left = quickSortDescOne($arr_left);    $arr_right = quickSortDescOne($arr_right);    return array_merge($arr_left, array($arr[$index]), $arr_right);}/** * @方案二 * @desc 快速排序算法ASC * @author lxs */function quickSortAscTwo(&$arr, $left, $right) {    $index = floor(($left + $right)/2);    $f = $arr[$index];      //分界值    $ltemp = $left;    $rtemp = $right;    while ($ltemp < $rtemp) {        while ($arr[$ltemp] < $f) {            ++$ltemp;        }        while ($arr[$rtemp] > $f) {            --$rtemp;        }        if ($ltemp <= $rtemp) {            $t = $arr[$ltemp];            $arr[$ltemp] = $arr[$rtemp];            $arr[$rtemp] = $t;            ++$ltemp;            --$rtemp;        }    }    if ($ltemp == $rtemp) {        $ltemp++;    }    if ($left < $rtemp) {        quickSortAscTwo($arr, $left, $ltemp-1);    }    if ($ltemp < $right) {        quickSortAscTwo($arr, $rtemp+1, $right);    }}/** * @方案二 * @desc 快速排序算法ASC * @author lxs */function quickSortDescTwo(&$arr, $left, $right) {    $index = floor(($left + $right)/2);    $f = $arr[$index];      //分界值    $ltemp = $left;    $rtemp = $right;    while ($ltemp < $rtemp) {        while ($arr[$ltemp] > $f) {            ++$ltemp;        }        while ($arr[$rtemp] < $f) {            --$rtemp;        }        if ($ltemp <= $rtemp) {            $t = $arr[$ltemp];            $arr[$ltemp] = $arr[$rtemp];            $arr[$rtemp] = $t;            ++$ltemp;            --$rtemp;        }    }    if ($ltemp == $rtemp) {        $ltemp++;    }    if ($left < $rtemp) {        quickSortDescTwo($arr, $left, $ltemp-1);    }    if ($ltemp < $right) {        quickSortDescTwo($arr, $rtemp+1, $right);    }}/** * @desc 测试 */$arr = array(23,13,33,44,53,4,6,22,489,2,2,3,2,65,89,320,54,360,11,999);$count = count($arr);$res_asc_one = quickSortAscOne($arr);$res_desc_one = quickSortDescOne($arr);quickSortAscTwO($arr, 0, $count-1);$arr2 = array(23,13,33,44,53,4,6,22,489,2,2,3,2,65,89,320,54,360,11,999);$count2 = count($arr2);quickSortDescTwO($arr2, 0, $count2-1);echo '<pre>';print_r($res_asc_one);print_r($res_desc_one);print_r($arr);print_r($arr2);echo '</pre>';


0 0