PHP Common sorting algorithm

Source: Internet
Author: User

Binary lookup must be sequential table  time complexity O (log2n)
function Halfsearch ($arr, $val) {
$cnt = count ($arr);
$start = 0;
$end = $cnt-1;
while ($start <= $end) {
$half = Ceil (($start + $end)/2);
if ($arr [$half] = = $val) {
return $half;
}elseif ($arr [$half] > $val) {
$end = $half-1;
}elseif ($arr [$half] < $val) {
$start = $half + 1;
}
}
return-1;

}
$n = Halfsearch ([1,3,6,7,8,9,11,15,17,29,30,31,32,33,34,35,44,41,42,43,44,45,46,50], 30);
Var_dump ($n);

//Order lookup time complexity O (n) 
function Sortsearch ($arr, $val) {
$flag = false;
$exist =-2;
foreach ($arr as $k = + $v) {
if ($v = = $val) {
$flag = true;
Break
}
}

if ($flag = = False) {
Return-1;
}else{
return true;
}
}

$n = Sortsearch ([1,3,6,7,8,9,11,15,17,29,30,31,32,33,34,35,44,41,42,43,44,45,46,50], +);
Var_dump ($n);


///bubble sort time complexity O (n^2) The idea is that a group of numbers (adjacent to two numbers) is compared, if the number is greater than the subsequent exchange, so the result of comparison is to move the largest number to the last position 
function Maopao ($arr) {
$cnt = count ($arr);     
for ($i =0; $i <= ($cnt-1); $i + +) {
$flag = 1;
for an array of length N, we need to sort the N-1 wheel, each I-wheel to compare n-i times
for ($j =0; $j < ($cnt-$i); $j + +) {
if ($arr [$j] > $arr [$j +1]) {
$flag = 0;
$tmp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $tmp;
}
}
if ($flag) {
//proving array order does not require looping
break;
}
}
return $arr;
}


$n = Maopao ([1,50,2,6,4,3,8,9,12]);
Var_dump ($n);
The second thought of bubbling, this is not practiced.  
The first round of exchange process: Take the array of the first bit-2 and 5 than, found that no I am small, skip, take-2 and 3 ratio, hair small without my small skip ...
Take-2 and 3 ratio, the hair is smaller than I, two exchange position, the next loop when the first bit of the array has changed, is-3. Well, think about it.
And then the loop is not over, continue to take the first bit of the array (-3), compared with the last 4 of the array, and exchange the next position ...
$numbers = Array ( -2, 5, 3, 1, -3,-4); for ($i =0; $i <count ($numbers); $i + +) {for ($j = $i +1; $j <count ($numbers); $j + +) { if ($numbers [$i] > $numbers [$j]) { $tmp = $numbers [$i]; $numbers [$i] = $numbers [$j]; $numbers [$j] = $tmp; } } Var_dump ($numbers);} Var_dump ($numbers); exit;

Reference link: http://www.cnblogs.com/shen-hua/p/5422676.html https://www.cnblogs.com/toxiaonan/archive/2017/11/29/ 7920757.html






PHP Common sorting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.