Home > Backend Development > PHP Tutorial > PHP implements quick sorting php array sorting php array reordering php bubble sort calculation

PHP implements quick sorting php array sorting php array reordering php bubble sort calculation

WBOY
Release: 2016-07-29 08:54:57
Original
1014 people have browsed it

Quick sort: In the unordered array $data, select any value as the comparison value, define i as the head retrieval index, j as the tail retrieval index, algorithm steps:

(1) Initialize the comparison value $value=$ data[0], $i=1, $j=count($data)-1

(2) First search from the end, judge whether $data[$j] is less than $value, if not, then $j- -, continue to search until you find coordinates

that are smaller than $value (3) At this time, start the head search to determine whether $data[$i] is greater than $value, if not, then $i++, continue to search until you find a coordinate that is smaller than $value. Coordinates with larger $value

(4) At this time, the values ​​​​of $data[$j] and $data[$i] are exchanged with each other, that is, the values ​​larger than $value are placed on the right, and the values ​​smaller than $value are placed on the left.

(5) Repeat 3 and 4 until $i==$j

(6) At this time, the one larger than $value has been placed on the right, and the smaller than $value has been placed on the left, and the coordinate position in the middle has been determined is $i, the middle value is $value, exchange the value of $data[$i] with the value of $data[0], because the middle value is $value, you need to move $value to the middle coordinate of the array

(7 ) The array is divided into two unordered arrays on the left and right, and then recursively execute 1-6 respectively until the array length is 1

Tips: The Chinese definition of quick sort will be clearer in Baidu

Code:

<?php
header("Content-type: text/html; charset=utf-8"); 

function quickSort($data, $startIndex, $endIndex){
	if($startIndex < $endIndex){
		$value = $data[$startIndex];  // 对比值
		$startT = $startIndex + 1;
		$endT = $endIndex;
		
		while ($startT != $endT) {
			// 找到比对比值小的坐标
			while ($data[$endT] > $value && $endT > $startT){
				$endT--;
			}

			// 找到比对比值大的左边
			while ($data[$startT] < $value && $startT < $endT){
				$startT++;
			}

			if($endT > $startT){
				$temp =$data[$startT];
				$data[$startT] = $data[$endT];
				$data[$endT] = $temp;
			}
		}
		
		// 防止数组已经排序好的情况
		if($data[$startT] < $value){
			$data[$startIndex] = $data[$startT];
			$data[$startT] = $value;
		}
		
		$data = quickSort($data, $startIndex, $startT - 1);
		$data = quickSort($data, $startT + 1, $endIndex);
		return $data;
	}else{
		return $data;
	}
}

$data = array(10, 5, 30, 22, 1, 42, 14, 34, 8, 13, 28, 36, 7);
$data = quickSort($data, 0, count($data) - 1);
var_dump($data);
Copy after login

The above introduces the implementation of quick sorting in PHP, including quick sorting and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template