Talk C Chestnut Bar (26th: C-language instance-bubble sort)

Source: Internet
Author: User


Ladies and gentlemen, crossing, the last time we were talking about two-point search examples, this time we say the example is: bubble sort. Gossip Hugh

The words return to a positive turn. Let's talk C chestnuts together!


Let's start by saying what sort is, the so-called ordering is to arrange the elements in the container according to certain rules. We're still like the one before

Sample a daily life examples to illustrate: Now the school is basically engaged in military training, I think we have also participated in military training, before the military training affirmation

Will let everyone line up, instructors will let everyone according to their height from low to high queuing, so that the platoon out of the queue is relatively neat. In the queue

In the process of using a sort, the students to participate in military training is the elements of the container, the rules of the order is the instructor set: According to height from low to high into

Line. The result of sorting is a neat military training team. I say, do you understand? According to the height from low to high row

How did the team proceed? Under the Taiwan crossing in question, crossing Mo Urgent, you ask the question is what we want to say today, in the writing procedure

There are several ways to sort, let's start with one of them: bubble sort.


the basic principle of bubbling sorting is to compare the two adjacent elements in the container sequentially, and if the two elements do not conform to the ordered rules, then the interchange

The position of these two elements in the container until all adjacent elements in the window conform to the collation.


the implementation steps for the bubbling sort are as follows:

1. From the container head to the end of the traversal of the container, the traversal process to remove the adjacent two elements for comparison;

2. If these two elements do not conform to the ordered rules, then the position of the two elements in the container is exchanged;

3. If these two elements conform to the ordered rules, then go back to step 1;

4. Repeat steps 1 through 3 until you have traversed all the elements in the container.


Crossing, the text does not write code, the detailed code put in my resources, you can click here to download the use. Bubble sort of

The advantages are easy to understand and easy to implement. The disadvantage is that performance is relatively low. I have optimized the bubbling sort in the code so that I can

To improve the performance of bubble sorting.


Everyone, crossing, let's talk about the bubble sort example here. I want to know what the following example, and listen to tell.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Talk C Chestnut Bar (26th: C-language instance-bubble sort)

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.