search
Homephp教程php手册PHP两种快速排序算法实例

PHP两种快速排序算法实例

Jun 13, 2016 am 09:14 AM
phpmainExamplefastsortarticlealgorithm

PHP两种快速排序算法实例

 这篇文章主要介绍了PHP两种快速排序算法实例,本文直接给出实现代码,分别使用递归法、迭代法实现,需要的朋友可以参考下

 

 

虽然在PHP这样的web应用开发中,我们不是太强调排序的重要性,因为PHP自身已经带了例如sort()等这样强大的排序函数,但是在一些重要的场合,例如某些高并发的场合,我想排序算法的影响已经不能忽略。所以在此介绍递归排序和迭代排序。

递归法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

* 递归法实现的快速排序

*/

function quicksort($seq)

{

$k = $seq[0];

$x = array();

$y = array();

for($i=1; $i

if($seq[$i]

$x[] = $seq[$i];

} else {

$y[] = $seq[$i];

}

}

$x = quicksort($x);

$y = quicksort($y);

return array_merge($x, array($k), $y);

} else {

return $seq;

}

}

迭代法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

/**

* 迭代法的快速排序

*/

function quicksortx(&$seq)

{

$stack = array($seq);

$sort = array();

while ($stack) {

$arr = array_pop($stack);

if(count($arr)

if(count($arr) == 1) {

$sort[] = &$arr[0];

}

continue;

}

$k = $arr[0];

$x = array();

$y = array();

$_size = count($arr);

for($i =1 ;$i

if($arr[$i]

$x[] = &$arr[$i];

} else {

$y[] = &$arr[$i];

}

}

!empty($y) && array_push($stack, $y);

array_push($stack, array($arr[0]));

!empty($x) && array_push($stack, $x);

}

return $sort;

}

使用:

?

1

2

3

4

5

6

7

8

9

10

/**

*产生一个随机数组

*/

for($i=0;$i

$testArr[]=mt_rand(0,100);

}

var_dump($testArr);

var_dump(quicksort($testArr));

 

var_dump(quicksortx($testArr));

Statement
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.