Home >Backend Development >PHP Tutorial >php bubble sort
I have been in contact with PHP for so long, and I have only used three kinds of sorting, bubble sorting, quick sorting, and barrel sorting. Let’s learn bubble sorting today: So what is bubble sorting? Just like bubbles in a river, bubbles surface one by one, and here are numbers one by one. Its principle is to repeatedly visit (traverse) the sequence to be sorted and compare adjacent ones. Of the two numbers, move the larger number to the right, and then traverse until all numbers complete the order from small to large. Each time the current maximum is compared, the remaining numbers are compared in the next round. Two loops are used to do it. The outer loop controls the number of rounds, and the inner loop controls the elements of comparison: Upload the code
In the process of bubbling, my ideas have been in other people’s thoughts. In the process of Baidu, I saw another method and thought it was good, so I wrote it:
During the writing process, I admired the latter way of writing very much. His thinking is very flexible, because the first way of writing is based on our normal thinking. It is very straightforward and I feel that the thinking is very interesting. |