Four basic algorithms for PHP

Source: Internet
Author: User

/* Bubbling algorithm: Results from small to large, the rule is similar to the wave-driven sand beach, the initial threshold is 0, after the initial first wave, if the left value is found to be larger than the right, the threshold is changed and the completion of the wave push, the reinitialization threshold of 0, so reciprocating, until there is no threshold change, indicating There is no need to change the threshold to push the wave.
* @para $arr incoming sorted array
* @return $NEWARR array after sorting
*/
function Maopao ($arr)
{
How many times is it?
for ($i = count ($arr)-1; $i >0; $i-) {
$flag = 0;
Compare the two adjacent numbers on each trip
for ($j = 0; $j < $i; $j + +) {
if ($arr [$j]> $arr [$j +1]) {
$temp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $temp;
$flag = 1;
}
}
if ($flag = = 0) {
Break
}
}
return $arr;
}

/** Quick Sort: by recursive implementation, the recursive sub-rule is: with the first element as the base, larger than the cardinality of the filter to the new-left array, smaller than the cardinality of the filter to the new to the right of the array----recursive call the rule to the sub-bottom, and then start back to layer merge (left array, cardinality, right array
* @param $arr
* @return Array
*/
function QuickSort ($arr)
{
Determine if you need to proceed
$length = count ($arr);
if ($length <= 1) {
return $arr;
}

Select the first element as a datum
$base _num = $arr [0];
Iterates through all elements except the ruler, and fits into two arrays in relation to size
Initialize two arrays
$left _array = Array (); Less than the baseline
$right _array = Array (); Greater than the baseline
for ($i =1; $i < $length; $i + +) {
if ($base _num > $arr [$i]) {
Put in the left array
$left _array[] = $arr [$i];
} else {
Put it on the right
$right _array[] = $arr [$i];
}
}
This function is recursively called on the left and right arrays by the same sort method.
$left _array = Quick_sort ($left _array);
$right _array = Quick_sort ($right _array);
Merge
Return Array_merge ($left _array, Array ($base _num), $right _array);
}

/** Insert sort (worm sort): Initialize the first element in the array as an element in the "established result array", and then take the value after the "established result array" in the "established result array" from the end to the beginning of the comparison, if the value does not have a trailing value large, it simulates the creep, so that the value to the "set of The head advances again with the element value of the their neighbourhood, so reciprocating, until the presence of a smaller than the value of the existence to stop the peristalsis; loop calls the rule until all the elements creep through to complete the sort.
* @param $arr
* @return Mixed
*/
function Insertsort ($arr)
{
$len =count ($arr);
for ($i =1; $i < $len; $i + +) {
$tmp = $arr [$i];
Inner loop control, compare and insert
for ($j = $i-1; $j >=0; $j-) {
if ($tmp < $arr [$j]) {
Find the inserted element to be small, swap position, swap the element behind with the previous element
$arr [$j +1] = $arr [$j];
$arr [$j] = $tmp;
} else {
If you encounter an element that does not need to be moved, because the array is already sorted, the preceding one does not need to be compared again.
Break
}
}
}
return $arr;
}


/** Select Sort: Has the enumeration idea, the virtual array is two parts, the initial first fraction group has only one element (with outer loop, unceasingly increases), repeatedly enumerates the second fractional group element (with outer loop, continuously decreases) the minimum value and the first fractional group the last element to swap the position, the transposition end is the sorting completes.
* @param $arr
* @return Mixed
*/
function Selectsort ($arr)
{
Double loop complete, outer control wheel count, inner control comparison number
$len =count ($arr);
for ($i =0; $i < $len-1; $i + +) {
First assume the position of the smallest value
$p = $i;

for ($j = $i +1; $j < $len; $j + +) {
$arr [$p] is the currently known minimum value
if ($arr [$p] > $arr [$j]) {
Compare, find smaller, record the position of the minimum value, and compare with the known minimum value at the next comparison.
$p = $j;
}
}
The location of the current minimum value has been determined and saved to $p. If the location of the minimum value is found to be different from the current assumed position $i, the position is interchangeable.
if ($p! = $i) {
$tmp = $arr [$p];
$arr [$p] = $arr [$i];
$arr [$i] = $tmp;
}
}
Return to final result
return $arr;
}

Four basic algorithms for PHP

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.