What are the functions of the two for loops of bubble sort? Can I explain it in the simplest words?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: I originally wanted to get a direct view: I found that Zhihu does not support the gif format. please give a link.

Http://upload.wikimedia.org/wikipedia/commons/3/37/Bubble_sort_animation.gif

For the "bubble sort" algorithm, the core is Bubble.

How to bubble? That is to say, the smallest one in the array goes up RiskThe process is Swap with other adjacent elements. This is an internal loop.

After a certain process OneThe smallest element emerges. if there are n elements in the array, it will take n-1 times. this is an external loop.

Appendix: Why does a comparison-based algorithm need to sort 5 elements seven times? Bubbling means that, after multiple rounds of comparison and exchange between two adjacent pairs, the smallest "risk" in the current array is placed at the top each time, and this number is excluded, and the remaining numbers repeat the process, finally, an ordered series is formed.
The first loop (external loop) is used to exclude the number.
The second loop (inner loop) is responsible for comparison and exchange between two

You can create an array on your own, simulate the code you want, and then understand it.
Think about a group of bubbles
One of the bubbles ran to a bubble girl and said, "you are better than the other girl when you come here. the girl said, wow, you are better. so he ran to the front of the bubble girl and said, big Brother, we are bigger than anyone else. He will be honest after seeing him. This is the for of the inner layer. the bubble is similar to everyone.
The bubble has just been honest, and another bubble has begun to be bigger than others. this is the outer for. every bubble will do something better than other bubbles. The first loop aims to bring the bubble you need to the top of the loop.
The second loop aims to take several chances to complete all bubbles.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.