我写的php快速排序函数输出有些问题,帮忙看看
<code>function qsort($arr){ if(!isset($arr[1])){ return $arr; } for ($i=0; $i =$arr[0]){ $rarr[] = $arr[$i+1]; } } $larr = qsort($larr); $larr[] = $arr[0]; $rarr = qsort($rarr); return array_merge($larr,$rarr); } print_r(qsort(array(411,21,333,666)));</code>
输出Array ( [0] => [1] => [2] => 21 [3] => 333 [4] => 411 [5] => 666 )
为什么前面多了2个空值?
回复内容:
<code>function qsort($arr){ if(!isset($arr[1])){ return $arr; } for ($i=0; $i =$arr[0]){ $rarr[] = $arr[$i+1]; } } $larr = qsort($larr); $larr[] = $arr[0]; $rarr = qsort($rarr); return array_merge($larr,$rarr); } print_r(qsort(array(411,21,333,666)));</code>
输出Array ( [0] => [1] => [2] => 21 [3] => 333 [4] => 411 [5] => 666 )
为什么前面多了2个空值?
for循环的结束条件是$i 然后你一定是关了报错,所以PHP没有报错,而是认为结果是空值继续执行下去了,所以排序的结果中混入了空值 正确: https://3v4l.org/CIKED
应该是array_merge的原因
为啥要这样写排序呢? 内置的数组排序函数可以用啊!<?php
function qsort($arr){
if(!isset($arr[1])){
return $arr;
}
$larr = $rarr = array(); //初始化变量,确保它一定存在
for ($i=1; $i < count($arr); $i++) { //不用$i+1,而是修改初始值为1
if($arr[$i]<$arr[0]){
$larr[] = $arr[$i];
}
if($arr[$i]>=$arr[0]){
$rarr[] = $arr[$i];
}
}
$larr = qsort($larr);
$larr[] = $arr[0];
$rarr = qsort($rarr);
return array_merge($larr,$rarr);
}
print_r(qsort(array(411,21,333,666)));
<code> function querySort($arr,$order='asc') {
if (count($arr)<1) {
return $arr;
}
$arr_left = $arr_right = array();
$val = $arr[0]; unset($arr[0]);
foreach ($arr as $key => $value) {
if ($order=='desc') {
if ($value < $val) {
$arr_left[] = $value;
} else {
$arr_right[] = $value;
}
} else {
if ($value > $val) {
$arr_left[] = $value;
} else {
$arr_right[] = $value;
}
}
}
$arr_left = querySort($arr_left,$order);
$arr_right = querySort($arr_right,$order);
return array_merge($arr_left,[$val],$arr_right);
}
$arr = querySort(array(411,21,333,666));
echo "<pre class="brush:php;toolbar:false">";
var_dump($arr);</code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
