1. Insert Sort
Thought:
Each time a data element to be sorted is inserted into a sequential sequence, the sequence remains orderly, knowing that the data elements to be sorted are all inserted.
Example:
[Initial key Words] [49] 38 65 97 76 13 27 49
j=2 (38) [38 49] 65 97 76 13 27 49
J=3 (65) [38 49 65] 97 76 13 27 49
J=4 (97) [38 49 65 97] 76 13 27 49
J=5 (76) [38 49 65 76 97] 13 27 49
J=6 (13) [13 38 49 65 76 97] 27 49
J=7 (27) [13 27 38 49 65 76 97] 49
J=8 (49) [13 27 38 49 49 65 76 97]
Complexity of Time:
If the goal is to arrange the sequence of n elements in ascending order, then the insertion sort is the best and worst case scenario. The best case is that the sequence is already in ascending order, in which case the comparison operation needs to be done (n-1). The worst case scenario is that the sequence is sorted in descending order, then there is a total of n (n-1)/2 times to be performed at this point. The assignment operation to insert a sort is the number of times the comparison operation is added (n-1). On average, the time complexity of inserting the sorting algorithm is O (n^2). Thus, the insertion sort is not suitable for applications with a large amount of data for sorting. However, if the amount of data that needs to be sorted is small, for example, if the magnitude is less than thousand, then inserting the sort is a good choice.
Code:
function Insert_sort ($arr) {
$count = count ($arr);
for ($i =1; $i < $count; $i + +) {
$tmp = $arr [$i];
$j = $i-1;
while ($arr [$j] > $tmp) {
$arr [$j +1] = $arr [$j];
$arr [$j] = $tmp;
$j--;
}
}
return $arr;
}
2. Select sort
Thought: Each trip selects the smallest (or largest) element from the data element to be sorted, placed in the final order of the ordered sequence, until all the data elements are sorted out.
Example:
[Initial key Words] [49 38 65 97 76 13 27 49]
First trip sorted after 13 [38 65 97 76 49 27 49]
Second trip sorted after 13 27 [65 97 76 49 38 49]
Third trip sort after 13 27 38 [97 76 49 65 49]
Four-trip sort after 13 27 38 49 [49 97 65 76]
Five-trip sort after 13 27 38 49 49 [97 97 76]
Six-trip sort after 13 27 38 49 49 76 [76 97]
Seventh trip sort after 13 27 38 49 49 76 76 [97]
Last sorted results 13 27 38 49 49 76 76 97
Complexity of Time:
Time complexity O (N2), unstable sequencing, suitable for smaller scale
Code:
Select sort (one-dimensional array)
function Select_sort ($arr) {
$count = count ($arr);
for ($i =0; $i < $count; $i + +) {
$k = $i;
for ($j = $i +1; $j < $count; $j + +) {
if ($arr [$k] > $arr [$j])
$k = $j;
if ($k! = $i) {
$tmp = $arr [$i];
$arr [$i] = $arr [$k];
$arr [$k] = $tmp;
}
}
}
return $arr;
}
3. Bubble sort
Thought:
22 compares the size of the data element to be sorted, and finds that two data elements are exchanged in reverse order until there are no reversed data elements.
Example:
49 13 13 13 13 13 13 13
38 49 27 27 27 27 27 27
65 38 49 38 38 38 38 38
97 65 38 49 49 49 49 49
76 97 65 49 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76 76
49 49 49 76 97 97 97 97
Complexity of Time:
The time complexity of the algorithm is O (N2). However, when the original keyword sequence is ordered, only one trip to the comparison ends, at which time the complexity is O (n).
Code:
Bubble sort (one-dimensional array)
function Bubble_sort ($array) {
$count = count ($array);
if ($count <= 0) return false;
for ($i =0; $i < $count; $i + +) {
for ($j = $count-1; $j > $i; $j-) {
if ($array [$j] < $array [$j-1]) {
$tmp = $array [$j];
$array [$j] = $array [$j-1];
$array [$j-1] = $tmp;
}
}
}
return $array;
}
4. Quick Sort
Thought:
By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
Example:
initial keyword [49 38 65 97 76 13 27 49]
After a trip sort [27 38 13] 49 [76 97 65 49]
Two times sorted [13] 27 [38] 49 [49 65]76 [97]
Three trips sorted after 13 27 38 49 49 [65]76 97
Final sorted results 13 27 38 49 49 65 76 97
Complexity of Time:
The fast-sorting body algorithm has a time operation of approximately O (log2n), and the calculated amount of the molecular area function is approximately o (n), so the total time complexity is O (nlog2n), which is obviously better than the Bubble sort O (N2). But the advantage of the algorithm is not absolute. Test analysis, when the original file keyword ordered, fast sorting time complexity is O (N2), in this case fast sorting unpleasant. And the bubble sort of this situation is O (n), but soon. Fast sorting is considered to be the best sort of sorting method in which the original file records the keyword unordered.
Code:
function Quick_sort ($array) {
if (count ($array) <= 1) return $array;
$key = $array [0];
$left _arr = Array ();
$right _arr = Array ();
for ($i =1; $i <count ($array); $i + +) {
if ($array [$i] <= $key)
$left _arr[] = $array [$i];
Else
$right _arr[] = $array [$i];
}
$left _arr = Quick_sort ($left _arr);
$right _arr = Quick_sort ($right _arr);
Return Array_merge ($left _arr, Array ($key), $right _arr);
}
Comparison and selection of several sorting algorithms
1. Select the factors to consider for the sorting method:
(1) Number of elements to be sorted n;
(2) The size of the information of the element itself;
(3) The structure and distribution of the keywords;
(4) The condition of the language tool, the size of the auxiliary space, etc.
2. Summary:
(1) If n is smaller (n <= 50), it can be either directly inserted or sorted directly. Since the record moving operation required for direct insertion of the sort is more than the direct selection, so when the information of the record itself is large, it is better to sort by direct selection.
(2) If the initial state of the file has been basically ordered by the keyword, the choice of direct insertion or bubble sort is advisable.
(3) If n is larger, it should be sorted by the time complexity O (nlog2n): Quick sort, heap sort, or merge sort. Fast sorting is considered to be the best method in the current comparison based internal sorting method.
(4) in the comparison based on the method, each time the size of the two keyword is compared, only two possible transitions, so you can use a binary tree to describe the comparison process, which can prove that: when the file's n keyword randomly distributed, any with the help of "comparison" sorting algorithm, at least O (nlog2n ) of the time.
(5) When the information of the record itself is large, in order to avoid the time-consuming movement of the record, the linked list can be used as the storage structure.
Sorting algorithm-php