


PHP quick sorting principle, implementation method and example analysis
This article mainly introduces the principle and implementation method of PHP quick sort, and analyzes the algorithm principle and specific implementation techniques of PHP quick sort in the form of examples. Friends in need can refer to it
The details are as follows:
<?php $n = array('13','14','55','10','54','2','79','106','89','90','22','60','111','77777','-110','-10','123'); function partition($n,$left,$right) { global $n; $pivot = $n[$left]; $lo=$left; $hi=$right+1; while($lo+1!=$hi) { if($n[$lo+1]<$pivot) $lo++; else if($n[$hi-1]>$pivot) $hi--; else{ $t=$n[$lo+1]; $n[$lo+1]=$n[$hi-1]; $n[$hi-1]=$t; $lo++; $hi--; } } $n[$left]=$n[$lo]; $n[$lo]=$pivot; return $lo; } function quicksort($n,$left,$right) { global $n; $dp = 0; if ($left<$right) { $dp=partition($n,$left,$right); quicksort($n,$left,$dp-1); quicksort($n,$dp+1,$right); } } quicksort($n,0,sizeof($n)-1); print_r($n); ?>
Quick sort is an improvement on bubble sort. Its basic idea is to divide the data to be sorted into two independent parts through one-way sorting. All the data in one part is smaller than all the data in the other part, and then the two parts of the data are sorted sequentially. For quick sorting, the entire sorting process can be performed recursively, so that the entire data becomes an ordered sequence.
Assuming that the array to be sorted is A[1]...A[N], first select any data (usually the first data) as the key data, and then put all numbers smaller than it In front of it, all numbers larger than it are placed behind it. This process is called one-position quick sort. The algorithm of quick sorting is:
1), set two variables I, J, when sorting starts, I: = 1, J: = N;
2), use the first array As key data, the element is assigned to X, that is, If there is a value less than ;
5), repeat steps 3 and 4 until I=J;
Quick sort is to recursively call this process - split the data sequence with 49 as the midpoint, and separate the previous part and The latter part performs similar quick sorting to complete the quick sorting of all data sequences, and finally turn this data sequence into an ordered sequence
Summary: The above is the entire content of this article, I hope it can be helpful to everyone learning helps.
Related recommendations:
phpDetailed explanation of WeChat development access example
php Detailed explanation of WeChat development access example
PHP MySQL implements fuzzy query employee information functionThe above is the detailed content of PHP quick sorting principle, implementation method and example analysis. 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



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.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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