1. Fastest and simplest sort, bucket sort
Question: Suppose there are 5 classmates, scored in one exam (out of 10): 3 5 8 2 5, how do you sort them in order from big to small?
Solution: Define a length of 11 array, that is, a[0]-a[10], subscript ordinal corresponding to the score 1-10; Each person gets a score of N, which makes a[n]++, if two people have 5 points, a[5]=2;
The array is then printed in sequence with a small ordinal number, and the corresponding value n is printed n times.
Graphic Interpretation:
+1
+1 +1 +1 +1
A[1] a[2] a[3] a[4] a[5] a[6] a7[] a[8] a[9] a[10]
The code is as follows:
#include <stdio.h>intMain () {inta[Ten] I J t; for(i=0; i<=Ten; i++) {A[i]=0;//each entry of the initialized array is 0 } for(i=1; i<=5; i++) {scanf ("%d", &t);//Loop input 5 charactersa[t]++;//array value corresponding to subscript +1 } for(i=0; i<=Ten; i++) {//iterate through the array, from small to large sort for(j=1; j<=a[i]; J + +) {//determine the value of each item and print a few times when it appearsprintf"%d", I)} } getchar (); GetChar (); return 0}
Summary: The advantages of bucket sequencing are very fast sorting;
The disadvantage is a waste of space, if you need to sort the number range very ah big: 1~999999999, then you need to apply 1 billion variables (barrels) a[999999999].
Application scenario: You need to know the number of times each variable appears.
2. Bubble sort
Basic idea: compare two adjacent elements at a time, and if they are in the wrong order, swap their positions.
Issue: Sort from large to small for 12 35 99 18 765 numbers.
Problem solving ideas: because it is from the big to the small sort, so the small number should be ranked behind. Compare only two numbers at a time to determine if the smaller number is on the right, or swap position;
After comparing 4 times, the smallest number in the array will appear on the far right, which we call "homing", a series of comparisons that we call "a trip"; (homing and trip are very important concepts!) )
After the end of a trip to start the second trip, compare 3 times, the second-to-last number will appear in the penultimate position ...
So the analogy.
Graphical interpretation: The smaller of the two numbers will go up, just like bubbling.
The code is as follows:
1#include <stdio.h>2 intMain () {3 inta[ -],i,j,t,n;4SCA9NF ("%d", &n)//Enter a number n to indicate that there is a total number of N to compare5 for(i=0; i<=n; i++){6scanf"%d", &a[i])//put the number loop in array a7 }8 9 for(i=1; i<=n-1; i++) {//We need to go n-1 a total.Ten for(j=2; j<=n-i;j++) {//each trip compares n-i times One if(A[j] < a[j+1]){//Judging, if the small number is on the left, then the swap position At =A[j]; -A[J] = a[j+1] -a[j+1] =T the } - } - } - + for(i=1; i<=n; i++) {//Loop output Array results -printf"%d", A[i]); + } A at GetChar (); - GetChar (); - return 0; -}
Summary: Bubble sort compares only two numbers at a time, starting with the first two of the series
If you compare the number of n, you need to carry out n-1, a total of n-1 number, until the last number of non-return, the arrangement to end
The first time, only need to compare n-i times, because there are already i-1 number of digits, do not need to be compared with the number of the return position
The core of the bubbling sort is the double nesting, the outer layer is n-1, the inner is this trip to be n-i times compared
Disadvantage, the bubble sort time complexity is very high.
Application scenario: Sorting out unordered columns
3. Quick Sort
Basic idea: Determine the base number k, which is generally the 1th number in a series. On both sides of the sequence to the middle of the "exploration", according to judging conditions (such as in order), starting from the right side, when a value greater than k on the right, the left to start
When the left one value is less than K, the left and right two values are exchanged, continue exploring until both sides traverse to the same value, and this value is exchanged with the reference value K.
Problem: Sort unordered Series 6 1 2 7 9 3 4 5 10 8.
Problem-solving ideas: Determine the base number K (set to 6 in the book), define I j two variables, respectively, from the sequence ends (A[0] and a[9]) to the middle, when J<k,i>k, A[i] and A[j] Exchange.
If descending, the judging condition is changed to, when J>k,i<k, will a[i] and A[j] Exchange.
Graphic Interpretation:
K I J
6 1 2 7 9 3 4 5 10 8
K I J
6 1 2 7 9 3 4 5 10 8
K I J at this point the j<k,i begins to traverse
6 1 2 7 9 3 4 5 10 8
K I J
6 1 2 7 9 3 4 5 10 8
K I J
6 1 2 7 9 3 4 5 10 8 variable I found a number that is larger than the base value, satisfies the j<k,i>k, and switches the two number
K I J
6 1 2 5 9 3 4 7 10 8
K I J
6 1 2 5 4 3 9 7 10 8
K i/j
6 1 2 5 4 3 9 7 10 8 Finally, I and J meet, then it and benchmark value Exchange K <==> i/j
3 1 2 5 4 6 9 7 10 8 The final result of this number
Then we will continue to use the same method to sort the left and right of 6.
AHA algorithm (i)