PHP Four classic sorting algorithm source code
--for reference only welcome reviews
<?php
Header ("content-type:text/html; Charset=utf-8 ");
// ****************************************************************
Quick sort: Each time you take a comparison value of $key (that is, the corresponding array of $arr[0]), then compare the number in the array, and then the comparison value is larger than the right and renamed to $right[], than the comparison value of the small left and renamed to $left[]. The recursive call function ends with $left[], $key, $right [] merge output.
function QuickSort ($arr) {
$n = count ($arr);
if ($n <1) {return $arr;}
Assigning values to comparison values
$key = $arr [0];
Declaring an array of left and right arrays
$left = Array ();
$right = Array ();
The other numbers in the array are compared to the comparison values and assigned to $left[] or $right[].
for ($i = 1; $i < $n; $i + +) {
if ($arr [$i] <= $key) {
$left [] = $arr [$i];
}else{
$right [] = $arr [$i];
}
}
Recursively calls the Quicksort () function to sort each sub-array.
$left = QuickSort ($left);
$right = QuickSort ($right);
Finally, $left[], $key, $right [] merge output.
Return Array_merge ($left, Array ($key), $right);
}
//****************************************************************
//bubble sort: compare and swap adjacent two numbers each time until the end of the comparison, That is, after each comparison, the current largest number is bubbled to the last. Altogether bubbled n-1 times.
Function Bubblesort ($arr) {
$n =count ($arr);
if ($n <1) {return $arr;} The
//bubbling number of cycles outside the loop n-1 times.
for ($i =0; $i < $n-1; $i + +) {
//the Inner Loop (n (i+1)) of the comparison and interchange cycles in each pass.
for ($j = $i +1; $j < $n; $j + +) {
//judge and swap large numbers to the following
if ($arr [$i]> $arr [$j]) {
$temp = $arr [$i];
$arr [$i] = $arr [$j];
$arr [$j] = $temp;
}
}
}
return $arr;
}
//****************************************************************
//Select sort: In the array, place the minimum number by comparison in the first position, Then put the remaining number of the smallest in the second position, knowing that the maximum will be placed at the end.
Function Selectsort ($arr) {
$n = count ($arr);
if ($n <1) {return $arr;} The
//outer loop first assumes that the first number is the smallest and assigns the position to $m
for ($i =0; $i < $n; $i + +) {
$m = $i;
//Inner loop to find the remaining minimum value and assign the position to $m
for ($j = $i +1; $j < $n, $j + +) {
if ($arr [$j]< $arr [$m]) {//$arr [$m] is dynamically changing and cannot be written as $[$i]!
$m = $j;
}
}
//Compare whether the minimum value found is not equal to the first number of the layer, the inequality is exchanged, otherwise the first one is the smallest.
if ($arr [$m]!= $arr [$i]) {
$temp = $arr [$m];
$arr [$m]= $arr [$i];
$arr [$i]= $temp;
}
}
return $arr;
}
// ****************************************************************
Insert Sort:
function Insertsort ($arr) {
$n = count ($arr);
if ($n <1) {return $arr;}
for ($i =1; $i < $n; $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;
}
Array definition, assignment
$arr 0 = array (23,43,45,434,-7687,990,5,111,23);
echo "Original array is: 434-7687 990 5 111 23<br/>";
1. Call Quick Sort
$arr 1 = quickSort ($arr 0);
echo "The result of quick sorting is:";
foreach ($arr 1 as $value) {
echo $value. " ";
}
echo "<br/>";
2. Call Bubble Sort
$arr 2 = Bubblesort ($arr 0);
echo "bubble sort result is:";
foreach ($arr 2 as $value) {
echo $value. " ";
}
echo "<br/>";
3. Call Select Sort
$arr 3=selectsort ($arr 0);
echo "Select sort the result is:";
foreach ($arr 3 as $value) {
echo $value. " ";
}
echo "<br/>";
4. Call Insert Sort
$arr 4=insertsort ($arr 0);
echo "Insert sort result is:";
foreach ($arr 4 as $value) {
echo $value. " ";
}
echo "<br/>";
?>
PHP Four classic sorting algorithm source code