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
- PHP基础排序算法(四)快速排序
- php四种基础排序(基础算法)----④快速排序法
- 排序算法(四)快速排序算法
- PHP四种基础排序算法--冒泡,直接插入,快速和选择排序
- 排序算法(四):快速排序
- 排序算法(四)-- 快速排序
- Java排序算法(四):快速排序
- 基础算法系列(四)——快速排序
- 快速排序----(排序算法四)
- 排序算法(四):快速排序
- php四种基础排序算法
- php 四种基础排序算法
- 基础算法之四--排序:之快速排序
- 四基础排序算法:插入排序、选择排序、冒泡排序、快速排序
- 算法与数据结构基础(四)高级排序算法2.快速排序
- 基础排序算法-快速排序
- php 快速排序算法
- PHP 快速排序算法
- 更改ssh服务远程登录的配置
- MySQL中针对大数据量常用技术:查询优化,数据转移
- 暑期打卡——Ubuntu下的java web开发环境搭建(eclipse+tomcat+mysql+jdk)
- Windows下配置Protobuf
- 单例模式(懒汉方式和饿汉方式)
- PHP基础排序算法(四)快速排序
- SDUT 3467 图的基本存储的基本方式四
- Quartz(07) quartz spring web 项目的整合(方法一)
- Git暂存区原理
- LeetCode | Construct Binary Tree from Preorder and Inorder Traversal
- Drupal7函数之drupal_get_messages()
- Hive集成HBase详解
- ReactNative之Flexbox布局
- 电磁场理论