大牛

WBOY
Release: 2016-06-13 10:18:16
Original
1049 people have browsed it

求救大牛
Array ( [0] => 16 [1] => 23 [2] => 34 [3] => 100 [4] => 12 [5] => 23 [6] => 31 [7] => 37 [8] => 55 )

?要求升序排列,不能使用内置函数.求算法

------解决方案--------------------
/*
?* 冒泡排序
?*/
function bubble_sort($array)
{
if(!is_array($array))
{
return false;
}

$len=count($array);

for($i=0;$i {
$flag = false;

for($j=0;$j {
if($array[$j] > $array[$j+1])
{
$temp=$array[$j];

$array[$j]=$array[$j+1];

$array[$j+1]=$temp;

$flag = true;
}
}
if(! $flag)//本趟排序没有值交换,则提前终止程序
? return $array;
}
return $array;
}
/*?
?* 希尔排序
?*/

function shell_sort($array)
{
if(!is_array($array))
{
return false;
}

$len=count($array);

$d=$len;//随机增量,初始值为数组长度,以不断除2取值

while($d >1)
{
$d=intval($d / 2);//分组间隔,2为n值,n值减少时,移动的趟数和数据增多

$temp=NULL;

$j=0;

for($i=$d;$i {
if($array[$i] {
$temp=$array[$i];

$j=$i-$d;

while(($j >=0) && $temp {
$array[$j+$d]=$array[$j];

$j = $j - $d;
}

$array[$j+$d]=$temp;
}
}
}
return $array;
}
/*
?* 选择排序
?*/
function select_sort($array)
{
if(!is_array($array))
{
return false;
}

$len=count($array);

for($i=0;$i {
$k=$i;

for($j=$i+1;$j {
if($array[$k] > $array[$j])
{
$k=$j;
}
}
if($i!=$k){
$temp = $array[$i];
? $array[$i] = $array[$k];
? $array[$k] = $temp;
}
}
return $array;
}
?>
------解决方案--------------------
选择排序,冒泡排序,插入排序
------解决方案--------------------
百度、排序算法,一抓一大堆
------解决方案--------------------
去学习下数据结构去

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!