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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值