PHP实现用迭代实现数组的快速排序
时间:2021-07-01 10:21:17
帮助过:27人阅读
- function quicksortX(&$seq)
- {
- $stack = array($seq);
- $sort = array();
- while ($stack) {
- $arr = array_pop($stack);
- if(count($arr) <= 1){
- if (count($arr) == 1) {
- $sort[] = &$arr[0];
- }
- continue;
- }
- $k = $arr[0];
- $x = array();
- $y = array();
- $_size = count($arr);
- for ($i = 1; $i < $_size; $i++) {
- if ($arr[$i] <= $k) {
- $x[] = &$arr[$i];
- } else {
- $y[] = &$arr[$i];
- }
- }
- !empty($y) && array_push($stack, $y);
- array_push($stack, array($arr[0]));
- !empty($x) && array_push($stack, $x);
- }
- return $sort;
- }
|
迭代, PHP