PHP's four basic sorting algorithms are: bubble sort, insert sort, select sort, and quick sort.
Here's the algorithm code I've compiled:
1. Bubble Sort:
Idea: Multiple rounds of bubbles in an array, each round of elements in an array of 22 comparison, adjust the position, out of a maximum number.
Simple version:
function Bubblesort ($arr)
{
$n = count ($arr);
for ($i =1; $i < $n; $i + +) {//bubbling number of wheels (max. $n-1 wheel)
for ($j =0; $j < $n-1; $j + +) {//bubble per round (22 comparison, large rear shift)
if ($arr [$j] > $arr [$j +1]) {//The former greater than the latter, swap position
$tmp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $tmp;
}
}
}
return $arr;
}
Improved version:
function Bubblesort ($arr)
{
$n = count ($arr);
for ($i =1; $i < $n; $i + +) {//bubbling number of wheels (max. $n-1 wheel)
$flag = 0; Whether a flag for position swapping occurs
for ($j =0; $j < $n-$i; $j + +) {//bubble per round (22 comparison, large rear shift)
if ($arr [$j] > $arr [$j +1]) {//The former greater than the latter, swap position
$tmp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $tmp;
$flag = 1;
}
}
if ($flag = = 0) {//No position exchange occurred, sort completed
Break
}
}
return $arr;
}
In order to improve the efficiency of the bubble sorting algorithm, the main areas to be improved are:
(1) The number of rounds to reduce bubbling: when a position swap is not occurring in a round bubble sort, the array is ordered and
That is, exit the loop.
(2) Reduce the number of comparisons per round: some of the elements that are already ordered in the array are no longer compared to them.
2. Insert Sort:
Idea: Assuming that the elements in front of the array are ordered, iterate through the elements behind the array, find the appropriate position in the queued elements queue, and insert them.
function Insertsort ($arr)
{
$n = count ($arr);
for ($i =1; $i < $n; $i + +) {//insert starting from the second element
for ($j = $i-1; $j >=0; $j-) {///compared to the previous number, locate the inserted position
if ($arr [$j] > $arr [$j +1]) {//smaller than the previous number, swap position
$tmp = $arr [$j];
$arr [$j] = $arr [$j +1];
$arr [$j +1] = $tmp;
} else {//is greater than or equal to the preceding number, indicating that the inserted position was found
Break
}
}
}
return $arr;
}
3. Select sort:
Idea: Make multiple selections, and select the maximum element to put in the specified position.
function Selectsort ($arr)
{
$n = count ($arr);
for ($i = $n-1; $i >0; $i-) {//Select the number of wheels to sort ($n-1 wheel)
$pos = $i; Assume the position of the largest element
for ($j =0; $j < $i; $j + +) {//round: Select the maximum number of elements that have never been selected
if ($arr [$j] > $arr [$pos]) {//position element is larger than the current largest element, marking its position
$pos = $j;
}
}
if ($pos! = $i) {//puts the largest element in the specified position
$tmp = $arr [$pos];
$arr [$pos] = $arr [$i];
$arr [$i] = $tmp;
}
}
return $arr;
}
4. Quick Sort:
Idea: recursive algorithm. The first element of the array is selected as the standard, and then the numbers less than or equal to it and the number greater than it are placed into two arrays, and the two arrays are treated the same, finally merging the two arrays and the first element.
function QuickSort ($arr)
{
$n = count ($arr);
if ($n <= 1) {//If the array has only one element, return directly
return $arr;
}
$LARGEARR = Array (); Large number of storage
$SMALLARR = Array (); Storing decimals
$cur = $arr [0]; Classification cardinality
for ($i =1; $i < $n; $i + +) {//iterate over array elements to categorize each element
if ($arr [$i] > $cur) {
$LARGEARR [] = $arr [$i];
} else {
$SMALLARR [] = $arr [$i];
}
}
Perform the same processing for large arrays and fractional groups, respectively
$SMALLARR = QuickSort ($SMALLARR);
$LARGEARR = QuickSort ($LARGEARR);
Merging decimal groups, categorical cardinality, and large arrays
Return Array_merge ($SMALLARR, Array ($cur), $LARGEARR);
}
Time complexity and spatial complexity for each sorting algorithm:
|
Sorting algorithms |
best time to analyze |
Worst-time analysis |
Average Time complexity |
Degree of stability |
Complexity of space |
|
Bubble sort |
O (N) |
O (N2) |
O (N2) |
Stability |
O (1) |
|
Insert Sort |
O (N) |
O (N2) |
O (N2) |
Stability |
O (1) |
|
Select sort |
O (N2) |
O (N2) |
O (N2) |
Stability |
O (1) |
|
Quick Sort |
O (NLOG2N) |
O (N2) |
O (NLOG2N) |
Not stable |
O (log2n) ~o (n) |
Note: Fast sorting in an array is the best efficiency in order to be efficient, and the efficiency is the worst when the array is ordered.
PHP four basic sorting algorithms