# Sorting algorithm in PHP

Premise: bubble sort, fast sort, select sort and insert sort are used to sort the values in the following array from small to large.

\$arr(1,43,54,62,21,66,32,78,36,76,39);

### 1. Bubble sorting

Train of thought analysis: in a group of numbers to be sorted, compare and adjust the two adjacent numbers from the front to the back for the sequence that has not been arranged at present, so that the larger number sinks and the smaller number rises. That is, whenever two adjacent numbers are compared and find that their sorting is opposite to the sorting requirements, they are exchanged.

Code implementation:

```\$arr=array(1,43,54,62,21,66,32,78,36,76,39);

function bubbleSort(\$arr)

{

\$len=count(\$arr);

//The number of rounds required for bubbling in the layer cycle control

for(\$i=1;\$i<\$len;\$i++)

{ //This layer loop is used to control the number of times to compare

for(\$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 a group of numbers to be sorted, select the smallest number and exchange the number of the first position. Then find the smallest number among the remaining numbers and exchange it with the number in the second position, so as to cycle until the last number is compared with the last number.

Code implementation:

```function selectSort(\$arr) {

//Double cycle completion, number of outer control rounds, number of inner control comparisons

\$len=count(\$arr);

for(\$i=0; \$i<\$len-1; \$i++) {

//Assume the location of the minimum value first

\$p = \$i;

for(\$j=\$i+1; \$j<\$len; \$j++) {

//\$arr[\$p] is the currently known minimum

if(\$arr[\$p] > \$arr[\$j]) {

//Compare, find smaller, record the location of the minimum value; and compare 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 it is found that the position of the minimum value is different from the current assumed position \$i, then the position can be interchanged.

if(\$p != \$i) {

\$tmp = \$arr[\$p];

\$arr[\$p] = \$arr[\$i];

\$arr[\$i] = \$tmp;

}

}

//Return final result

return \$arr;

}```

### 3. Insert sort

Train of thought analysis: in a group of numbers to be sorted, assume that the numbers in front are already in order. Now insert the nth number into the ordinal number in front, so that the nth number is also in order. Repeat the cycle until all are in order.

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 smaller, and the position is exchanged. The element at the back is exchanged with the element at the front

\$arr[\$j+1] = \$arr[\$j];

\$arr[\$j] = \$tmp;

} else {

//If you encounter elements that do not need to be moved, because they have been sorted and are arrays, the previous ones do not need to be compared again.

break;

}

}

}

return \$arr;

}

```

### 4. Quick sort

Thinking analysis: select a benchmark element, usually the first element or the last element. Through a scan, the sequence to be arranged is divided into two parts, one is smaller than the reference element, the other is greater than or equal to the reference element. At this time, the benchmark element is in the correct position after it is sorted, and then the two parts are sorted recursively by the same method.

Code implementation:

```function quickSort(\$arr) {

//First, judge whether it is necessary to continue

\$length = count(\$arr);

if(\$length <= 1) {

return \$arr;

}

//Select the first element as the base

\$base_num = \$arr;

//Traverse all elements except the ruler and put them into two arrays according to the size relationship

//Initialize two arrays

\$left_array = array();  //Less than base

\$right_array = array();  //Greater than base

for(\$i=1; \$i<\$length; \$i++) {

if(\$base_num > \$arr[\$i]) {

//Put in the left array

\$left_array[] = \$arr[\$i];

} else {

//Put on the right side

\$right_array[] = \$arr[\$i];

}

}

//Then call this function recursively by using the same sorting method for the left and right arrays

\$left_array = quick_sort(\$left_array);

\$right_array = quick_sort(\$right_array);

//merge

return array_merge(\$left_array, array(\$base_num), \$right_array);

}```

Keywords: less

Added by steveangelis on Sun, 08 Dec 2019 03:07:53 +0200