Summary and discussion of three kinds of methods used in C language

Source: Internet
Author: User
Tags comparison rounds sort

Sorting is a very important part of program design, it is the function of a group of disorderly data, arranged into an orderly sequence of data, after the arrangement of data, either from the large to small arrangement, or from small to large arrangement. There are generally only two situations.

For example, we count the grades of students, then generally according to the school number to carry out statistics, the original results are unordered arrangement, so that is not very suitable for our results of the query, then generally we do the results of the query, first, such as according to the ranking of high to low points, In this way, we can quickly find out the highest and lowest points in this class, and the students who are better than before or after the result.

There are many ways to sort, there are three kinds of commonly used: bubble sort, select sort, insert sort, etc., below we do analysis and comparison to these three kinds of methods, so that we can better understand and apply.

First, bubble sort

1, bubble sort of the basic idea: for the number of N to sort (now assumed to be from large to small sort, the following are followed by the next two numbers are compared, the large number will be placed in the front: that is, the first number and the second number are compared, the large number put before the decimal, the second and the third to compare, the large number put before, decimal place, Then, and so on ... After the first round of comparison, we find a minimum number below (bottom). Then the next round of comparison, the last number will not have to participate in the comparison, so this round can be compared less once.

Obviously, we need to use the double loop to design this problem, the outer loop control of the number of rounds, the inner loop to control the number of times per round, then exactly how many rounds, how many times per round, we have an example to look at:

2, sorting process examples:

Outer circulation

1 rounds

2 rounds

3 rounds

4 rounds

Inner Loop

5 numbers compared 4 times

4 numbers compared 3 times

3 numbers compared 2 times

2 numbers compared 1 times

7

5

8

6

9

1 times

2 times

3 times

4 times

1 times

2 times

3 times

1 times

2 times

1 times

7

5

8

6

9

7

8

5

6

9

7

8

6

5 /p>

9

7

8

6

9

5

8

7

6

9

5

8

7

6

9 /p>

5

8

7

9

6

5

8

7

9

6

5

8

9

7

6 /p>

5

9

8

7

6

5

The smallest number 5 to the bottom, the remaining 4 to compare

Sub decimal 6 Bottom, the remaining 3 number

7 Bottom, the other 2 numbers compare

Last two number of comparisons

So through this sort of process, we know how to do the sort, then exactly who is the bubble, we can find out the answer, then from large to small to sort, the larger number is the bubble. As the sort progresses, the bubbles gradually rise.

From this sort of seed, you can also see that 5 of the actual number of 4 rounds can be, the practice has proved that the number of n is required to n-1 the wheel sort.

3, bubble sort of procedures are as follows:

for (i=0;i<10;i++)

for (j=0;j<10-i;j++)

if (a[j]<a[j+1])

{t=a[j];a[j]=a[j+1];a[j+1]=t;}

Add the input section above the program section and the output after the program segment is sorted.

Improvements to the program:

4, the improvement of the algorithm:

From the above sort of process you can see that if a set of ordered group of numbers or after a small number of rounds can be completed, but the loop is still going on, so that the design of the program wasted a lot of time, so we can redesign the algorithm.

The modified process is as follows:

for (i=0;i<10&&!swap;i++)

{

Swap=1;

for (j=0;j<10-i;j++)

if (a[j]<a[j+1])

{t=a[j];a[j]=a[j+1];a[j+1]=t;swap=0;}

}

Second, select the sort

1, the basic idea of sorting: starting from the first number, compare the first number with the number of other numbers, and swap the position if it is larger than the first number, so that after the first round we will be able to find the maximum value in the first position, and then look for a larger number from the second position, in turn, Can be a whole number of sorting, practice has proved that the number of n is required to n-1 the wheel sort.

2, sorting process examples:

Outer circulation

1 rounds

2 rounds

3 rounds

4 rounds

Inner Loop

5 numbers compared 4 times

4 numbers compared 3 times

3 numbers compared 2 times

2 numbers compared 1 times

7

5

8

6

9

1 times

2 times

3 times

4 times

1 times

2 times

3 times

1 times

2 times

1 times

7

5

8

6

9

8

5

7

6

9

8

5

7

6 /p>

9

9

5

7

6

8

9

7

5

6

8

9

7

5

6 /p>

8

9

8

5

6

7

9

8

6

5

7

9

8

7

6 /p>

5

9

8

7

6

5

The largest number 9 found, the remaining 4 to find a large number of times

Number of times 8 found, the remaining 3 to find

7 Find, the other 2 number find

Last two number of comparisons

The choice of sorting is easier to understand than bubbling, and program writing is relatively easy.

for (i=0;i<10;i++)

for (j=i+1;j<10;j++)

if (A[i]<a[j])

{t=a[i];a[i]=a[j];a[j]=t;}

For selection sorting, we can also see a problem, as in the first sort, we're looking for a 9 to be the maximum, so the other exchanges are completely unnecessary, and so are the other rounds, so we can find a way to undo the situation, which means we actually find the maximum position and then swap it.

for (i=0;i<10;i++)

{p=i;

for (j=i+1;j<10;j++)

if (A[p]<a[j])

P=j;

if (p!=i)

{t=a[i];a[i]=a[j];a[j]=t;}

}

The algorithm has been improved to solve this problem better.

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.