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
9
|
7
8
6
9
5
|
8
7
6
9
5
|
8
7
6
9
5
|
8
7
9
6
5
|
8
7
9
6
5
|
8
9
7
6
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:
Copy Code code 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:
Copy Code code 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
9
|
9
5
7
6
8
|
9
7
5
6
8
|
9
7
5
6
8
|
9
8
5
6
7
|
9
8
6
5
7
|
9
8
7
6
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.
Copy Code code as follows:
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.
Copy Code code as follows:
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.
Third, insert sort
1, insert the basic idea of sorting: (assuming from large to small sort) in turn from the back of a number and the previous row of the number to compare, the process of comparison is to start with the last number in a sorted number, and if you continue to compare it to the previous one, you will find a number larger than it, and then put it behind it. If it has not been found, it is certain that the number has been compared to the first number, then put it in front of the first number.
In general, for a set of numbers that are sorted by the insertion sort method, you can select the first number as a set number that has been ordered. And put the second one in the right place.
2. The procedure is prepared as follows:
Copy Code code as follows:
The for (i=1;i<10;i++)//i can be started from 0 or 1. Other unchanged.
for (j=i;j>0;j--)
if (A[j]<a[j-1])
{t=a[j];a[j]=a[j-1];a[j-1]=t;}
There is a place for this program to be repaired, the order of the above program is actually based on the exchange of ideas to sort, but also to the true meaning of the sort, namely: first the number to be sorted out, and then find the location should be inserted, after the location to be inserted after the data all moved back, The original backlog data has been removed and placed in a temporary variable. Then insert the data into the correct free position.
Then for the exchange based insert ordering, no location was found before the exchange, so we can also proceed to improve the program. Then this program improvement, certainly can not reduce the number of exchanges, because we know if to find a place to exchange, then certainly has been looking for chaos of the original order results, so can only find location, teng position, put elements of these procedures.
Copy Code code as follows:
Main ()
{
int i,j,t,a[]={12,11,2,3,6,67,89,0,1,3};
for (i=1;i<10;i++)
{T=a[i];
J=i-1;
while (J>=0&&t>a[i])
{A[J+1]=A[J];
j--;
}
a[j+1]=t;
for (i=0;i<10;i++)
printf ("%d", a[i]);
printf ("n");
}
The above is a discussion of several sorting methods, on the sorting problem, is a very important content in the program design, so in the "data structure and algorithm" as an important content to do a thorough explanation, we here only do a simple discussion, in case of C language beginners or learning C language programming enthusiasts use.