回复讨论(解决方案)
$base_num 不会被覆盖,
这与 php 的变量作用域有关,函数内的变量只在本函数内有效
也就是说,递归调用的 quick_sort 中的 $base_num 都是独立的
这一点与 js 不同,需要注意。当然 js 中可用 var 声明局部变量
每次执行都是函数体内的局部变量,除非是静态或全局变量才会改变
$base_num 不会被覆盖,
这与 php 的变量作用域有关,函数内的变量只在本函数内有效
也就是说,递归调用的 quick_sort 中的 $base_num 都是独立的
这一点与 js 不同,需要注意。当然 js 中可用 var 声明局部变量
你的意思是,我举个例子,比如:3,1,5,4
第一次穿进去把3拿出来做标尺,1,5,4以此和它做对比,小于它的在left数组,大于它的在right数组,$left=array(1); $right=array(5,4);
当$right再次递归时候,把5拿出来做标尺,4和它对比。那被做标尺的值3和5最后能在最后一行代码
return array_merge($left_array, array($base_num), $right_array);中找到,是这样吧。
这样看的清楚点
print_r(quick_sort(array(6,1,5,8,7,4,2,3,0)));function quick_sort($arr) { //先判断是否需要继续进行 $length = count($arr); if($length <= 1) { return $arr; } //如果没有返回,说明数组内的元素多于 1 个,需要排序 //选择一个标尺 //选择第一个元素 $base_num = $arr[0]; //遍历 除了标尺外所有元素,按照大小关系放入两个数组内 //初始化两个数组 $left_array = array(); //小于标尺的 $right_array = array(); //大于标尺的 for($i=1; $i<$length; $i++) { if($base_num > $arr[$i]) { //放入左边数组 $left_array[] = $arr[$i]; } else { //放入右边数组 $right_array[] = $arr[$i]; } }printf("in[%s] B[%d] L[%s] R[%s]<br>", join(',', $arr), $base_num, join(',', $left_array), join(',', $right_array)); //再分别对 左边 和 右边 得得数组进行相同的排序处理 //递归调用这个函数,并记录结果 $left_array = quick_sort($left_array); $right_array = quick_sort($right_array); //合并左边、标尺、右边 return array_merge($left_array, array($base_num), $right_array);}
in[6,1,5,8,7,4,2,3,0] B[6] L[1,5,4,2,3,0] R[8,7]in[1,5,4,2,3,0] B[1] L[0] R[5,4,2,3]in[5,4,2,3] B[5] L[4,2,3] R[]in[4,2,3] B[4] L[2,3] R[]in[2,3] B[2] L[] R[3]in[8,7] B[8] L[7] R[]Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 )
这样看的清楚点
print_r(quick_sort(array(6,1,5,8,7,4,2,3,0)));function quick_sort($arr) { //先判断是否需要继续进行 $length = count($arr); if($length <= 1) { return $arr; } //如果没有返回,说明数组内的元素多于 1 个,需要排序 //选择一个标尺 //选择第一个元素 $base_num = $arr[0]; //遍历 除了标尺外所有元素,按照大小关系放入两个数组内 //初始化两个数组 $left_array = array(); //小于标尺的 $right_array = array(); //大于标尺的 for($i=1; $i<$length; $i++) { if($base_num > $arr[$i]) { //放入左边数组 $left_array[] = $arr[$i]; } else { //放入右边数组 $right_array[] = $arr[$i]; } }printf("in[%s] B[%d] L[%s] R[%s]<br>", join(',', $arr), $base_num, join(',', $left_array), join(',', $right_array)); //再分别对 左边 和 右边 得得数组进行相同的排序处理 //递归调用这个函数,并记录结果 $left_array = quick_sort($left_array); $right_array = quick_sort($right_array); //合并左边、标尺、右边 return array_merge($left_array, array($base_num), $right_array);}
in[6,1,5,8,7,4,2,3,0] B[6] L[1,5,4,2,3,0] R[8,7]in[1,5,4,2,3,0] B[1] L[0] R[5,4,2,3]in[5,4,2,3] B[5] L[4,2,3] R[]in[4,2,3] B[4] L[2,3] R[]in[2,3] B[2] L[] R[3]in[8,7] B[8] L[7] R[]Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 )
这样看的清楚点
print_r(quick_sort(array(6,1,5,8,7,4,2,3,0)));function quick_sort($arr) { //先判断是否需要继续进行 $length = count($arr); if($length <= 1) { return $arr; } //如果没有返回,说明数组内的元素多于 1 个,需要排序 //选择一个标尺 //选择第一个元素 $base_num = $arr[0]; //遍历 除了标尺外所有元素,按照大小关系放入两个数组内 //初始化两个数组 $left_array = array(); //小于标尺的 $right_array = array(); //大于标尺的 for($i=1; $i<$length; $i++) { if($base_num > $arr[$i]) { //放入左边数组 $left_array[] = $arr[$i]; } else { //放入右边数组 $right_array[] = $arr[$i]; } }printf("in[%s] B[%d] L[%s] R[%s]<br>", join(',', $arr), $base_num, join(',', $left_array), join(',', $right_array)); //再分别对 左边 和 右边 得得数组进行相同的排序处理 //递归调用这个函数,并记录结果 $left_array = quick_sort($left_array); $right_array = quick_sort($right_array); //合并左边、标尺、右边 return array_merge($left_array, array($base_num), $right_array);}
in[6,1,5,8,7,4,2,3,0] B[6] L[1,5,4,2,3,0] R[8,7]in[1,5,4,2,3,0] B[1] L[0] R[5,4,2,3]in[5,4,2,3] B[5] L[4,2,3] R[]in[4,2,3] B[4] L[2,3] R[]in[2,3] B[2] L[] R[3]in[8,7] B[8] L[7] R[]Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 )
这个很直观!谢谢~

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


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

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
