Home > php教程 > PHP源码 > 简单的冒泡排序

简单的冒泡排序

PHP中文网
Release: 2016-05-25 16:59:30
Original
1369 people have browsed it

php代码

<?php
	/**
	| 简单的描述一下冒泡排序方法
	| 将数组中的数字从小到大排列
	**/
	
class buff_order
{
	public function to_order($array)
	{
		$k = 0;


		for ($i = 0; $i < count($array); $i++)
		{
			$max = $i+1;

			if (isset($array[$max]))
			{
				if($array[$i] > $array[$max])
				{
					$tmp = $array[$i];
					$array[$i] = $array[$max];
					$array[$max] = $tmp;
					$k++;
				}
			}
			
			if($k > 0)
			{
				$array = $this->to_order($array);
			}
		}
		
		return $array;
	}
}
$array = array(9,8,6,5,3,2,4,1,7);
$obj = new buff_order;
print_r($obj->to_order($array));
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template