Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.
It repeatedly visits the column of elements to sort, compares two adjacent elements in turn, and swaps them if their order (e.g., from large to small, the first letter from A to Z) is wrong. The work of a visiting element is repeated until no adjacent elements need to be exchanged, that is, the element has been sorted.
The name of this algorithm is because the larger the elements will slowly "float" to the top of the series (ascending or descending), as the bubbles of carbon dioxide in carbonated drinks will eventually rise to the top, hence the name "bubble sort".
This is the encyclopedia of "bubble sort" interpretation, the basic principle is: Compare the size of the adjacent elements in a set of data, in ascending order, the value of the small forward, the value of the person backward, until all the elements from small to large display. when sorting an array of any length count, the following rule is found: the count-1 wheel needs to be sorted, and each I-wheel needs to be compared count-i times. This uses a double loop, the outer control loop, and the inner layer controls the number of comparisons per round.
The code is as follows:
Test code:
Results:
Relive the bubble sort of PHP