Java_ Bubble sorting_ Principle and optimization

Bubble sorting and its optimization

I Principle and optimization principle

  1. Principle explanation

Bubble sorting: the first number is compared with the second number. If the condition is satisfied and the position remains unchanged, the second number is compared with the third number If the conditions are not met, replace the position, and then compare the second number with the third number, and so on. The execution is completed into one trip, and the number of trips is equal to the number of comparisons minus one

  2. Schematic diagram of bubble sorting principle: (taking 98765 sequence as an example, the sorting result is from small to large)

  3. Bubble sorting optimization

Optimized version: reduce one cycle at a time (i.e. the red part does not need to be compared)

  4. Bubble sort final

Final version: reduce one cycle per trip (delete line does not need to be executed again)

II Implementation code

  1. Bubble sort implementation of the main code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>