PHP implements the sorting algorithm in the data structure. Bubble sort [basic principle] compares two adjacent numbers in sequence, placing decimals in front, and placing large numbers in the back. At the end of the first round, the maximum number is placed at the end. The second stop will be
Bubble sorting
[Basic principle]
The adjacent two numbers are compared in sequence, the decimal places are placed in front, and the large numbers are placed behind. At the end of the first round, the maximum number is placed at the end. The second step ends, putting the maximum number to the second to the last. Continue until the sorting is completed.
Bubble sorting, which only needs to be implemented using two repeated loops, with the time complexity of O (n * n ).
[Code implementation]
Implementation: compare the values in two to put a small number in front.
$ Array [$ j + 1]) {$ temp = $ array [$ j]; $ array [$ j] = $ array [$ j + 1]; $ array [$ j + 1] = $ temp; $ flag = true ;}} if (! $ Flag) // if there is no value exchange in this sort, the program return $ array ;}// call the implementation of $ array = array (7, 2, 4, 1, 6, 3, 8, 0, 5); print_r (bubble_sort ($ array);?>
Hill Sorting
[Basic principle]
Take an integer d1 smaller than n as the first increment, and divide all records of the file into d1 groups. All distance is d1
Multiple records are placed in the same group. Insert and sort directly in each group. then, take the second incremental d2. < p>
Group and sort the above until the incremental dt = 1 (dt <…< p><…
Insert directly until sorting.
[Code implementation]
Implementation: compare with the number of incremental intervals until the large number is placed at the end
1) {$ d = intval ($ d/2); // grouping interval. 2 is the value of n. when the value of n is reduced, the number of moving shards and data increase. $ temp = NULL; $ j = 0; for ($ I = $ d; $ I <$ len; $ I + = $ d) {if ($ array [$ I] <$ array [$ I-$ d]) {$ temp = $ array [$ I]; $ j = $ I-$ d; while ($ j> = 0) & $ temp <$ array [$ j]) {$ array [$ j + $ d] = $ array [$ j]; $ j = $ j-$ d ;}$ array [$ j + $ d] = $ temp ;}}return $ array ;}$ array = array (7, 2, 4, 1, 6, 3, 8, 0, 5); print_r (shell_sort ($ array);?>
Select sort
[Basic principle]
Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged. Selecting sorting is an unstable sorting method.
[Code implementation]
Implementation: two cycles are used to compare and find the key of the smallest element (index in the array), and determine the current number and the minimum key. if they are not equal, they are the exchange value.
$ Array [$ j]) {$ k = $ j ;}} if ($ I! = $ K) {$ temp = $ array [$ I]; $ array [$ I] = $ array [$ k]; $ array [$ k] = $ temp;} return $ array;} $ array = array (7, 2, 4, 1, 6, 3, 8, 0, 5); print_r (select_sort ($ array);?>
Source: http://meego123.net /? Posting = 126
Explain [basic principle] compares two adjacent numbers in sequence, placing decimals in front, and placing large numbers in the back. At the end of the first round, the maximum number is placed at the end. The second stop will be the largest...