Rumah >pembangunan bahagian belakang >masalah PHP >Ensiklopedia pengisihan algoritma PHP, algoritma yang mesti dibaca untuk temu duga
1.冒泡排序
isihan fungsi($array) {
$length=count($array);
untuk ($i=$length; $i>0;$i--){
$swape=true;
untuk ($j=0;$j<$i-1;$j ){
if ($array[$j]>$array[$j 1]){
$temp=$array[$j];
$ =$array[$j 1];
$array[$j 1]=$temp;
$swape=false;
if ($swape)break; } return $array;}2.气泡isihan fungsi($input)
{
if(!isset($input))
{
Kembali [];
}
do
$ swapped = false;
untuk ($ i = 0, $count = sizeof($input) - 1; $i < $count; $i ) { if($input[$i 1] < $input[$i ]) { senarai($input[$i 1], $input[$i]) = [$input[$i], $input[$i 1]]; $swapped = benar; } } } } } } }
<🎜 );
<🎜 pulangkan $input;}$array = [51158,1856,8459,67957,59748,58213,90876,39621,66570,64204,79935,476935,476935 ,74474,63968,4337,43688,42685,31577,5239,25385,56301,94083,23232,67025,44044,74813,34671,90235,90235,90235,4047 620,38265,12973,25236,93144 ,13720,4204,77026,42348,19756,97222,78828,73437,48208,69858,19713,29411,49809,66174,52526277,7032877,70325 6818,86897,18456,29373,7853 ,24932,93070,4696,63015,9358,28302,3938,11754,33679,18492,91503,63395,12029,23954,27230,58434,33679,18492,91503,63395,12029,23954,27230,58436,23648,6368 29,96145,57954,32392,76201 | 4,46704,99211,55295,33963 ,77977,86805,72686];$array = sort($array);echo implode(',', $array);3.计数排序isihan fungsi($array, $min, $max){ $count = array(); untuk($i = $ min; $i <= $maks; $i ) { $count[$i] = 0; } foreach($array sebagai $nombor) { $count[$number] ; } $z = 0; untuk($i = $min; $i <= $maks; $i ) { manakala( $count[$i]-- > 0 ) { $array[$z ] = $i;} } return $array;}4.插入排序)function sort { untuk ($i=1;$i
5.快速排序
isihan fungsi($input)
{
if(!isset($input))
{
kembalikan [];
}
$lt = [];
$gt = [];
if(sizeof($input) < 2)
{
return $input;
}
$key = key $input);
$shift = array_shift($input);
foreach($input as $value)
{
$value < = $shift ? $lt[] = $value : $gt[] = $value;
}
return array_merge(quickSort($lt), [$key => $shift], quickSort( $gt));
}
$array = [51158,1856,8459,67957,59748,58213,90876,39621,66570,64204,79935,27892,47615,94706,344716,344716,344706,344706 8,42685,31577,5239 ,25385,56301,94083,23232,67025,44044,74813,34671,90235,65764,49709,12440,21165,20620,38265,12973,9265,12973 7026,42348,19756,97222,78828 ,73437,48208,69858,19713,29411,49809,3015,9358,28302,3938,11754,33679,18492,91503,63395,120249,236349,236349,2364 06,61349,37348,78629,96145 | 6,96782,19074,46574,46704 ,99211,55295,33963,77977,86805,72686];
$array = quickSort($array);
echo implode(',', $array);
6.选择排序
isihan fungsi($array){
$length=count($array);
untuk ($i=0;$i< $length;$i ){
$lowest=$i;
untuk ($j=$i 1;$j<$length;$j ){
if ($array[$j] < $array[$lowest]){
$lowest=$j;
}
}
$temp=$array[$i];
$array[$i]=$array[$lowest];
$array[$lowest]=$temp;
}
}
pulangkan $array;
}
————————————————
版权声明:本文为CSDN博主「m0_37540251」的原创文章,遵循记行记行博 , 遵循记语 , 遵循记记行行论附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37540251/article/details/125201836
Atas ialah kandungan terperinci Ensiklopedia pengisihan algoritma PHP, algoritma yang mesti dibaca untuk temu duga. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!