This article describes four basic sorting algorithms in PHP, which help scholars learn more about sorting. Eg: bubble sort, quick sort, select sort, insert sort
1. Bubble sorting
Train of Thought analysis:In the number Group to be sorted, compare and adjust the adjacent two numbers in sequence for the sequence that is not yet sorted, so that the larger number will sink, A relatively small amount of data goes up. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.
Code implementation:
$ Arr = array (, 54,62, 66, 39); function bubbleSort ($ arr) {$ len = count ($ arr ); // This layer controls the number of rounds to be bubbling for ($ I = 1; $ I <$ len; $ I ++) {// This layer loop is used to control the number of times a number needs to be compared in each round ($ k = 0; $ k <$ len-$ I; $ k ++) {if ($ arr [$ k]> $ arr [$ k + 1]) {$ tmp = $ arr [$ k + 1]; $ arr [$ k + 1] = $ arr [$ k]; $ arr [$ k] = $ tmp ;}} return $ arr ;}2. select sort
Train of Thought analysis:In the number of groups to be sorted, select the minimum number and the number at the first position. Then find the smallest number in the remaining number and exchange it with the number in the second position, so that the loop ends until the last number is compared with the last number.
Code implementation:
Function selectSort ($ arr) {// double loop completion, number of outer control wheels, number of inner control comparisons $ len = count ($ arr); for ($ I = 0; $ I <$ len-1; $ I ++) {// first assume the location of the smallest value $ p = $ I; for ($ j = $ I + 1; $ j <$ len; $ j ++) {// $ arr [$ p] is the currently known minimum value. if ($ arr [$ p]> $ arr [$ j]) {// comparison, it is found that, record the location of the minimum value, and compare it with the known minimum value in the next comparison. $ P = $ j ;}/// the location of the current minimum value has been determined and saved to $ p. If you find that the location of the minimum value is different from the current position $ I, you can change the location. If ($ p! = $ I) {$ tmp = $ arr [$ p]; $ arr [$ p] = $ arr [$ I]; $ arr [$ I] = $ tmp;} // return the final result return $ arr ;}3. Insert sorting
Train of Thought analysis:In the number of groups to be sorted, assuming that the previous number is already sorted in order, we need to insert the nth number into the previous ordered number so that the n number is also sorted in order. This repeats until all the rows are sorted.
Code implementation:
Function insertSort ($ arr) {$ len = count ($ arr); for ($ I = 1, $ I <$ len; $ I ++) {$ tmp = $ arr [$ I]; // inner loop control, compare and insert for ($ j = $ I-1; $ j >=0; $ j --) {if ($ tmp <$ arr [$ j]) {// It is found that the inserted element is small and the switch position is, swap the following elements with the preceding elements $ arr [$ j + 1] = $ arr [$ j]; $ arr [$ j] = $ tmp ;} else {// If you encounter elements that do not need to be moved, because they are sorted as arrays, you do not need to compare them again. Break ;}}return $ arr ;}4. quick sorting
Train of Thought analysis:Select a base element. generally, select the first or last element. After a scan, the columns to be sorted are divided into two parts. one part is smaller than the benchmark element, and the other part is greater than or equal to the benchmark element. At this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort the divided two parts.
Code implementation:
Function quickSort ($ arr) {// first judge whether to continue $ length = count ($ arr); if ($ length <= 1) {return $ arr ;} // select the first element as the reference $ base_num = $ arr [0]; // traverses all elements except the ruler, put the two arrays according to the size relationship // initialize the two arrays $ left_array = array (); // less than the benchmark $ right_array = array (); // for ($ I = 1; $ I <$ length; $ I ++) {if ($ base_num> $ arr [$ I]) {// Put $ left_array [] = $ arr [$ I];} else {// Put $ right_array [] = $ arr [$ I] on the right;} // recursively call this function $ left_array = quick_sort ($ left_array); $ right_array = quick_sort ($ right_array ); // merge return array_merge ($ left_array, array ($ base_num), $ right_array );}
The above describes the details of the four basic sorting algorithms in PHP. For more information, see other related articles in the first PHP community!