PHP four basic algorithms: Bubble, select, insert, and fast sorting method
Many people have said that the algorithm is the core of the program, a program is better than the poor, the key is the pros and cons of this program algorithm. As a primary phper, although little contact with algorithmic aspects. But for bubble sort, insert sort, select sort, and quickly sort four basic algorithms, I think it's still to be mastered. The following is my own understanding of the four methods analyzed.
Requirements: Bubble sort method, fast sorting method, select sorting method, insert Sort method to sort the values in the following array in the order of the small.
$arr (1,43,54,62,21,66,32,78,36,76,39);
1. Bubble Sort Method
* Thinking Analysis: The law, as its name, is like bubbling, each time from the array to take one of the largest number out.
* For example: 2,4,1//The first time the bubble is 4
* 2,1,4//The second time the bubble is 2
* 1,2,4//finally became like this
* Code implementation:
Copy Code code as follows:
$arr =array (1,43,54,62,21,66,32,78,36,76,39);
Function Getpao ( $arr)
{
$len =count ($arr);
//sets an empty array to receive Bubbles
//the number of rotations required for the layer to be bubbled
for ($i =1; $i < $len; $i + +)
{//This layer loop is used to control the number of times a number needs to be compared for each round
for ($k =0; $k < $len-$i; $k + +)
& nbsp; {
if ($arr [$k]> $arr [$k +1])
{
$ tmp= $arr [$k +1];
$arr [$k +1]= $arr [$k];
$arr [$k]= $tmp;
}
}
}
return $arr;
}
2. Select the sorting method:
Choice of sorting method: Each time you select a corresponding element, and then put it in the specified location
Copy Code code as follows:
function Select_sort ($arr) {
Realization of the double loop, the outer control wheel number, the current minimum value. The number of internal control comparisons
$i the position of the current minimum value, the elements that need to participate in the comparison
For ($i =0, $len =count ($arr); $i < $len-1; $i + +) {
Assume the position of the smallest value first
$p = $i;
$j are currently required to compare with which elements, $i behind.
for ($j = $i +1; $j < $len; $j + +) {
$arr [$p] is the currently known minimum value
if ($arr [$p] > $arr [$j]) {
Compare, find a smaller, recorded position of the minimum value, and in the next comparison,
A known minimum value should be used for comparison.
$p = $j;
}
}
The position of the current minimum value has been determined and saved to the $p.
If the location of the lowest value is found to be different from the current assumed position $i, the position interchange can be
if ($p!= $i) {
$tmp = $arr [$p];
$arr [$p] = $arr [$i];
$arr [$i] = $tmp;
}
}
Return final Result
return $arr;
}
3. Insert Sort method
Insert Sort idea: Inserts the element to be sorted into the specified position of an array that has been assumed to be a sort number.
Copy Code code as follows:
function Insert_sort ($arr) {
To distinguish which part is already sorted.
Which part is not sorted
Find one of the elements that needs to be sorted
This element starts with the second element, and the last element is the element that needs to be sorted.
Use loops to mark out
I loop control every time you need to insert elements, once you need to insert the elements control well,
Indirectly, the array has been divided into 2 parts, the subscript is less than the current (left), is a sorted sequence
For ($i =1, $len =count ($arr); $i < $len; $i + +) {
Gets the element value that you want to compare currently.
$tmp = $arr [$i];
Inner loop control Compare and insert
for ($j = $i-1; $j >=0; $j-) {
$arr [$i];//elements that need to be inserted; $arr [$j];//elements that need to be compared
if ($tmp < $arr [$j]) {
Found the inserted element to be small, swap position
Swap the elements that are behind with the preceding elements
$arr [$j +1] = $arr [$j];
Set the previous number to the current number that you want to swap
$arr [$j] = $tmp;
} else {
If you encounter elements that you do not need to move
Since the array is already sorted, the front does not need to be compared again.
Break
}
}
}
Inserts this element into the sorted sequence.
Return
return $arr;
}
4. Quick Sort Method
Copy Code code as follows:
function Quick_sort ($arr) {
Determine if you need to proceed
$length = count ($arr);
if ($length <= 1) {
return $arr;
}
If not returned, the number of elements in the array is more than 1, you need to sort
Select a ruler
Select the first element
$base _num = $arr [0];
Iterate over all elements except the ruler, and put them in two arrays by size
Initialization of two arrays
$left _array = Array ()//less than the ruler
$right _array = Array ()//greater than the ruler
for ($i =1; $i < $length; $i + +) {
if ($base _num > $arr [$i]) {
Put an array to the left
$left _array[] = $arr [$i];
} else {
Put it on the right.
$right _array[] = $arr [$i];
}
}
And then the same sort of treatment for the left and right arrays.
Recursively call this function and record the result
$left _array = Quick_sort ($left _array);
$right _array = Quick_sort ($right _array);
Merge right of left ruler
Return Array_merge ($left _array, Array ($base _num), $right _array);
}