PHP实现快速排序
来源:互联网 发布:淘宝用户名起名大全 编辑:程序博客网 时间:2024/05/24 01:54
public function actionQuick_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($this->actionQuick_sort($left), array($pivot), $this->actionQuick_sort($right)); } } public function actionTest() {// $arr = array(6,8,7,9,0,1,3,2,4,5);// // print_r($arr);// $this->actionQuicksort($arr,0,9);// print_r($arr);// $unsorted = array(43,21,2,1,9,24,2,99,23,8,7,114,92,5); $unsorted = array(6,8,7,9,0,1,3,2,4,5); $unsorted = array(10,2,36,14,10,25,23,85,99,45); $sorted = $this->actionQuick_sort($unsorted); print_r($sorted); }
0 0
- PHP实现快速排序
- php实现快速排序
- PHP实现快速排序
- PHP实现快速排序
- 快速排序PHP实现
- PHP实现快速排序
- PHP实现快速排序
- php实现快速排序
- PHP实现快速排序
- php实现快速排序
- PHP实现快速排序
- PHP实现快速排序
- PHP实现快速排序
- php实现快速排序
- PHP实现快速排序
- PHP实现快速排序算法
- php快速排序的实现
- 004.PHP实现快速排序
- 数组和指针的区别(a和&a以及&a[0]的区别)
- java对excel表的操作,jxl的基本操作
- 剑指offer(十三)之链表中倒数第k个结点
- Linux学习-杂
- iOS上传ipa
- PHP实现快速排序
- 关于Linux下的写时复制
- FLV文件格式解析
- Android开源项目分类汇总
- IntelliJ IDEA手工安装插件方法
- java二维码工具类,中间带LOGO的,很强大
- 英国监管机构向比特币抛出橄榄枝,管制环境更加宽松
- 关于Android内存泄漏的优秀文章
- H5页面快速搭建之高级字体应用实践