PHP实现冒泡算法

WBOY
Release: 2016-06-23 13:41:54
Original
885 people have browsed it

我发现很多公司面试的时候都会出一道冒泡算法的题,这么基础的算法,一般程序员应该都写得出来,但是今天我在网上看了一下,发现很多文章中的冒泡算法都是错的,根本就不是冒泡算法!还堂而皇之的说什么解析冒泡算法,真是误导新人啊。我就直接上代码了,这,才是冒泡算法。


<?phpfunction bubbleSort($ary){	$len = count($ary);	for ($i=0; $i < $len - 1; $i++) { 		for ($j=0; $j < $len - 1 -$i; $j++) { 			if ($ary[$j] > $ary[$j + 1]) {				$tmp = $ary[$j];				$ary[$j] = $ary[$j + 1];				$ary[$j + 1] = $tmp;			}		}	}	return $ary;}$ary = [1,6,789,3,56,8,1234,45];$newAry = bubbleSort($ary);print_r($newAry);?>
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!