An example of how to implement quick sort recursively in PHP
This article mainly introduces the method of PHP recursive implementation of quick sorting. It briefly describes the principle of quick sorting and analyzes the relevant operating skills of PHP using recursive algorithms to implement quick sorting in the form of examples. Friends in need can refer to the following
The example in this article describes the method of implementing quick sort recursively in PHP. Share it with everyone for your reference, the details are as follows:
First we need to understand the principle of quick sort:Find any element in the current array (generally select the first element ), as a standard, create two empty arrays and traverse the entire array elements. If the traversed element is smaller than the current element, then put it in the array on the left, otherwise put it in the array on the right, and then do the same with the new array. operation.
It is not difficult to find that this is consistent with the principle of recursion, so we can use recursion to implement it.
To use recursion, you need to find the recursion point and recursion exit:
Recursion point: If the elements of the array are greater than 1, then It needs to be decomposed again, so our recursion point is that the number of newly constructed array elements is greater than 1
Recursive exit:When do we no longer need to process a new array? No more sorting? That's when the number of array elements becomes 1, so this is our exit.
Understand the principle, let’s take a look at the code implementation~
<?php //快速排序 //待排序数组 $arr=array(6,3,8,6,4,2,9,5,1); //函数实现快速排序 function quick_sort($arr) { //判断参数是否是一个数组 if(!is_array($arr)) return false; //递归出口:数组长度为1,直接返回数组 $length=count($arr); if($length<=1) return $arr; //数组元素有多个,则定义两个空数组 $left=$right=array(); //使用for循环进行遍历,把第一个元素当做比较的对象 for($i=1;$i<$length;$i++) { //判断当前元素的大小 if($arr[$i]<$arr[0]){ $left[]=$arr[$i]; }else{ $right[]=$arr[$i]; } } //递归调用 $left=quick_sort($left); $right=quick_sort($right); //将所有的结果合并 return array_merge($left,array($arr[0]),$right); } //调用 echo "<pre class="brush:php;toolbar:false">"; print_r(quick_sort($arr));
Running results:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 6 [7] => 8 [8] => 9 )
PS: Here is another demonstration tool about sorting recommended for your reference:
##Online animation demonstration insertion/selection/bubble/merge/Hill/quick sort algorithm process tool:
http://tools.jb51.net/aideddesign/paixu_ys
Detailed tutorial on how to implement git deployment in PHP
Dichotomy of PHP implementation Search algorithm example analysis and explanation
Example explanation of the half search algorithm implemented in PHP
The above is the detailed content of An example of how to implement quick sort recursively in PHP. For more information, please follow other related articles on the PHP Chinese website!

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



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

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

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

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
