<?php
/**
* Created by Phpstorm.
* user:63448
* DATE:2018/5/5
* time:22:42
*/
$arr = [3,1,13,5,7,11,2,4,14,9,150,6,12,10,8];
Bubble sort
/**
* Thinking Analysis: In order to sort a group of numbers, the current is not well-arranged sequence, from the back to the adjacent two number of consecutive comparisons and adjustments,
* Let the larger number go down and take the smaller. That is, whenever two of neighboring numbers are compared, they are found to be in reverse order.
* Just swap them out. Code implementation:
*/
function Bubblesort ($arr) {
$length = count ($arr); Array length
for ($i = 1; $i < $length; $i + +) {//forward traversal array
for ($j = $length-1; $j >= $i; $j-) {//Reverse traversal
if ($arr [$j] < $arr [$j-1]) {//adjacent two number comparison
$iTemp = $arr [$j-1]; Staging a smaller number
$arr [$j-1] = $arr [$j]; Large put in front
$arr [$j] = $iTemp; The smaller put back
}
}
}
return $arr;
}
Var_dump (Bubblesort ($arr));
/**
* 2. Select sort
* Thinking Analysis: In the group of numbers to be sorted, select the smallest number to exchange the number of the first position.
* Then in the remaining number, find the smallest and second position of the number exchange, so loop to the penultimate number and the last number comparison.
*/
function Choicesort ($arr) {
$length = count ($arr); Array length
for ($i = 0; $i < $length-1; $i + +) {//traversal array
$iTemp = $arr [$i]; Staging Current value
$iPos = $i; Staging Current Location
for ($j = $i + 1; $j < $length; $j + +) {//traverse the data after the current position
if ($arr [$j] < $iTemp) {//If there is less than the current value
$iTemp = $arr [$j]; Temporary minimum value
$iPos = $j; Staging Location
}
}
$arr [$iPos] = $arr [$i]; Put the current value in the calculated position
$arr [$i] = $iTemp; Change the current value to a calculated value
}
return $arr;
}
Var_dump (Choicesort ($arr));
/**
* 3. Insert Sort
* Thinking Analysis: In the group of numbers to be sorted, assuming that the previous number is already in the order, now to insert the nth number in the preceding ordinal number,
* so that the n number is also in order. This cycle is repeated until all the rows are in order.
*/
function Insertsort ($arr) {
$length = count ($arr);
for ($i = 1; $i < $length; $i + +) {//traversal array
$iTemp = $arr [$i]; Get Current value
$iPos = $i-1; Get the previous position of the current value
while (($iPos >= 0) && ($iTemp < $arr [$iPos])) {//If the current value is less than the previous value is not tangent to the start position of the array
$arr [$iPos + 1] = $arr [$iPos]; Put the value of the previous one back.
$iPos--; Decreasing position
}
$arr [$iPos +1] = $iTemp;
}
return $arr;
}
Var_dump (Insertsort ($arr));
/**
* 4. Quick Sort
* Thinking Analysis: Select a datum element, usually select the first element or the last element.
* Through a scan, the waiting sequence is divided into two parts, which is smaller than the datum element and part is greater than or equal to the datum element.
* At this point the datum element is in the correct position after it is ordered, and then the same method is used recursively to sort the divided two parts.
*/
function QuickSort ($arr) {
$length = count ($arr);
$l = $r = 0;
$left = $right = Array ();
Iterate through the array from the second start of the index
for ($i = 1; $i < $length; $i + +) {
If the value is less than index 1
if ($arr [$i] < $arr [0]) {
Mount the left-indexed array (data less than index 1)
$left [] = $arr [$i];
$l + +;
} else {
Otherwise, load in the right index (data greater than index 1)
$right [] = $arr [$i];
$r + +; //
}
}
Sort the left index if the left index has a value
if ($l > 1) {
$left = QuickSort ($left);
}
The sorted array
$new _arr = $left;
Puts the first of the current array to the last
$new _arr[] = $arr [0];
Sort the right index if the index has a value
if ($r > 1) {
$right = QuickSort ($right);
}
Add data again based on the length of the right index
for ($i = 0; $i < $r; $i + +) {
$new _arr[] = $right [$i];
}
return $new _arr;
}
Var_dump (QuickSort ($arr));
/**
* 5. Hill sort
* Thinking Analysis: Separates the array into several sub-sequences by the specified step, and then sorts the sub-sequences separately (in this is direct)
*/
function Shellsort ($arr) {
$len = count ($arr);
$k = Floor ($len/2);
while ($k > 0) {
for ($i = 0; $i < $k; $i + +) {
for ($j = $i; $j < $len, ($j + $k) < $len; $j = $j + $k) {
if ($arr [$j] > $arr [$j + $k]) {
$tmp = $arr [$j + $k];
$arr [$j + $k] = $arr [$j];
$arr [$j] = $tmp;
}
}
}
$k = Floor ($k/2);
}
return $arr;
}
Var_dump (Shellsort ($arr));
/**
* 6. Heap Sorting
* Thinking Analysis: The process of adjusting the sub-heap for Dagen
* @param array & $arr arrays
* @param int $s The location of the root of the sub-heap
* @param int $m The last element position of the heap
* @return Array
*/
Function Heapadjust (& $arr, $s, $m) {
$tmp = $arr [$s];
The left or right child heap may be affected in the process of tuning to Dagen
The purpose of the For loop is to ensure that the sub-heap is also Dagen
for ($j = $s + 1, $j <= $m; $j = $j + 1) {
Find the largest of the left and right children of the root node and compare it with the root node,
If the big is adjusted, otherwise meet the characteristics of Dagen jump out of the loop
if ($j < $m && $arr [$j] < $arr [$j +1]) {
$j + +;
}
if ($tmp >= $arr [$j]) {
Break
}
$arr [$s] = $arr [$j];
$s = $j;
}
$arr [$s] = $tmp;
}
Heap Sort
function Heapsort ($arr) {
$len = count ($arr);
Adjust the heap to a large heap starting from the child heap
for ($i = Floor ($len/2-1); $i >= 0; $i-) {
Heapadjust ($arr, $i, $len-1);
}
Switch the root node to the last position, then resize the heap to a large heap again, and find the secondary maximum value.
Get an ordered array by analogy
for ($n = $len-1; $n > 0; $n-) {
$tmp = $arr [$n];
$arr [$n] = $arr [0];
$arr [0] = $tmp;
Heapadjust ($arr, 0, $n-1);
}
return $arr;
}
Var_dump (Heapsort ($arr));
/**
* 7. Sorting by Exchange method
* @param array $arr
* @return Array
*/
function Swapsort ($arr) {
$length = count ($arr);
Iterating through an array
for ($i = 0; $i < $length-1; $i + +) {
Gets the next index of the current index
for ($j = $i + 1; $j < $length; $j + +) {
Compare the value size of adjacent two
if ($arr [$j] < $arr [$i]) {
Staging a smaller number
$iTemp = $arr [$i];
Large put in front
$arr [$i] = $arr [$j];
The smaller put back
$arr [$j] = $iTemp;
}
}
}
return $arr;
}
Var_dump (Swapsort ($arr));
/**
* 8. Merge sort
* Thinking Analysis: Here is a two-way merger (the Arr1[s will be ordered separately.) M], ARR2[M+1..N] Merge into ordered $arr2[s. N])
* @param array & $arr 1
* @param array & $arr 2
* @param int $s
* @param int $m
* @param int $n
*/
Function Merge (& $arr 1, & $arr 2, $s, $m, $n) {
for ($k = $s, $i = $s, $j = $m +1; $i <= $m && $j <= $n; $k + +) {
if ($arr 1[$i]< $arr 1[$j]) {
$arr 2[$k] = $arr 1[$i + +];
}else {
$arr 2[$k] = $arr 1[$j + +];
}
}
if ($i <= $m) {
for (; $i <= $m; $i + +) {
$arr 2[$k + +] = $arr 1[$i];
}
}else if ($j <= $n) {
for (; $j <= $n; $j + +) {
$arr 2[$k + +] = $arr 1[$j];
}
}
}
Recursive form of two-way merging
Function Msort (& $arr 1, & $arr 2, $s, $t) {
if ($s = = $t) {
$arr 2[$s] = $arr 1[$s];
}else {
$m = Floor (($s + $t)/2);
$tmp _arr = Array ();
Msort ($arr 1, $tmp _arr, $s, $m);
Msort ($arr 1, $tmp _arr, $m +1, $t);
Merge ($tmp _arr, $arr 2, $s, $m, $t);
}
}
* Two-way merging of elements in an array $arr[0..n-1]
function MergeSort ($arr) {
$len = count ($arr);
Msort ($arr, $arr, 0, $len-1);
return $arr;
}
Var_dump (MergeSort ($arr));
Bubble sort http://www.yduba.com/biancheng-3962519476.html
//
Select Sort http://www.yduba.com/biancheng-4732577282.html
//
Insert Sort http://www.yduba.com/biancheng-2892530909.html
//
Hill sort http://www.yduba.com/biancheng-5472542087.html
//
Merge sort http://www.yduba.com/biancheng-9902519493.html
//
Quick Sort http://www.yduba.com/biancheng-0362533007.html
//
Heap Sort http://www.yduba.com/biancheng-4622548289.html
//
Calculate Sort http://www.yduba.com/biancheng-1362548964.html
//
Bucket sort http://www.yduba.com/biancheng-7382573665.html
//
Base sort http://www.yduba.com/biancheng-6212544194.html
------Ordering of PHP algorithms