Home php教程 php手册 php 数组排序:php实现各种排序

php 数组排序:php实现各种排序

Jun 21, 2016 am 08:51 AM
function list temp


/**
* 各种排序
* @author zhaojaingwei
* @since 2011/11/21 16:14
*
*/
$list = array(3,5,1,2,10,8,15,19,20);
//快排
function fast(&$list, $low, $high){
if($high - $low > 5){
while($low $key = excute($list, $low, $high);
fast($list, $low, $key - 1);
//fast($list, $key + 1, $high);//普通递归实现
$low = $key + 1;//尾递归实现
}
}else{
insert($list);
}
}
//快排执行一次排序
function excute(&$list, $low, $high){
swap($list, $low, ($low + $high)/2);
$temp = $list[$low];
while($low while($low $temp){
$high --;
}
$list[$low] = $list[$high];
while($low $low ++;
}
$list[$high] = $list[$low];
}
$list[$low] = $temp;
return $low;
}
//堆排序
function heap(&$list){
buildheap($list);
for($i = count($list) - 1; $i > 0; $i --){
swap($list, $i, 0);
heapfy($list, 0, $i - 1);
}
}
//创建堆
function buildheap(&$list){
for($i = (count($list) - 2)/2; $i >= 0; $i --){
heapfy($list, $i, count($list) - 1);
}
}
//维护堆
function heapfy(&$list, $low, $high){
$temp = $list[$low];
for($i = ($low * 2 + 1); $i if($i $i ++;
}
if($temp swap($list, $i, $low);
$low = $i;
}else{
break;
}
}
$list[$low] = $temp;
}
//希尔排序
function shell(&$list){
$a = 0;
$code = count($list)/3 + 1;
while($code >= 1){
for($i = $code; $i $a ++;
if($list[$i] $temp = $list[$i];
$list[$i] = $list[$i - $code];
$j = $i - 2*$code;
for(; $j >= 0 && $list[$j] > $temp; $j -= $code){
$list[$j + $code] = $list[$j];
$a ++;
}
$list[$j + $code] = $temp;
}
}
$code = $code/3;
}
echo $a;
}
//直接插入排序
function insert(&$list){
$a = 0;
for($i = 1; $i $a ++;
if($list[$i] $temp = $list[$i];
$list[$i] = $list[$i - 1];
$j = $i - 2;
for(; $list[$j] > $temp; $j --){
$a ++;
$list[$j + 1] = $list[$j];
}
$list[$j + 1] = $temp;
}
}
echo $a;
}
//简单选择排序
function select(&$list){
$a = 0;
for($i = 0; $i $min = $i;
$a ++;
for($j = $i + 1; $j $a ++;
if($list[$j] $min = $j;
}
}
if($min != $i)
swap($list, $i, $min);
}
echo $a;
}
//冒泡排序
function bubble(&$list){
$swap = true;
$a = 0;
for($i = 0; $i $swap = false;
$a ++;
for($j = count($list) - 2; $j >= $i; $j --){
$a ++;
if($list[$j] > $list[$j + 1]){
$swap = true;
swap($list, $j, $j + 1);
}
}
}
echo $a;
}
//移动或交换函数
function swap(&$list, $i, $j){
$temp = $list[$i];
$list[$i] = $list[$j];
$list[$j] = $temp;
}
?> 本文链接http://www.cxybl.com/html/wlbc/Php/20120607/28506.html



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Teach you how to modify the temporary file location of Win7 Teach you how to modify the temporary file location of Win7 Jan 04, 2024 pm 11:25 PM

The temp folder is our temporary file storage location. The system will save temporary files to this folder. If there are too many temporary files, especially when the temp folder is on the system disk, it is likely to affect the system running speed. We can solve the problem by changing the temp location. Let’s take a look below. Tutorial on changing the location of win7temp 1. First, right-click "Computer" and open "Properties" 2. Click "Advanced System Settings" on the left 3. Click "Environment Variables" below 4. Select "temp" and click "Edit" 5. Then change Just change the "Variable Value" to the path that needs to be changed.

What does temp mean in computer? What does temp mean in computer? Sep 29, 2021 pm 04:39 PM

In computers, temp means "temporary folder", which contains many temporary files; its function is to temporarily save the user's work results in the application to prevent losses caused by accidents. Deleting temp files can free up hard disk storage space, but it will be slower to open the application for the first time after deletion.

How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] Apr 17, 2023 am 08:13 AM

Most of the devices, such as laptops and desktops, have been heavily used by young gamers and coders for a long time. The system sometimes hangs due to application overload. This forces users to shut down their systems. This mainly happens to players who install and play heavy games. When the system tries to boot after force shutdown, it throws an error on a black screen as shown below: Below are the warnings detected during this boot. These can be viewed in the settings on the event log page. Warning: Processor thermal trip. Press any key to continue. ..These types of warning messages are always thrown when the processor temperature of a desktop or laptop exceeds its threshold temperature. Listed below are the reasons why this happens on Windows systems. Many heavy applications are in

Internal error: Unable to create temporary directory [Resolved] Internal error: Unable to create temporary directory [Resolved] Apr 17, 2023 pm 03:04 PM

Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

How to implement Redis List operation in php How to implement Redis List operation in php May 26, 2023 am 11:51 AM

List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

What file is temp? What file is temp? Jan 05, 2021 am 10:43 AM

temp is a temporary folder. In the path "C:\Documents and Settings\Administrator\Local Settings\", many temporary files are placed here, including favorites, temporary files for browsing web pages, editing files, etc.

How to convert JSONArray to List in Java How to convert JSONArray to List in Java May 04, 2023 pm 05:25 PM

1: JSONArray to ListJSONArray string to List//Initialize JSONArrayJSONArrayarray=newJSONArray();array.add(0,"a");array.add(1,"b");array.add(2,"c") ;Listlist=JSONObject.parseArray(array.toJSONString(),String.class);System.out.println(list.to

See all articles