Beginner sort, also more vague, more is tiger, can not understand thoroughly. Today make a summary of several simple sorts. Continue to learn and summarize more, better performance in the future!
First, select the sort
Put the code on first.
1#include <stdio.h>2 intMain ()3 {4 inta[9] = {5,7,4,6,1,9,3,2,8};5 intLen =sizeof(a)/sizeof(int);6 inti,j,min,temp,m;7 for(i =0; i < Len; i++) 8 {9Min =i;Ten for(j = i+1; J < Len; J + +) One { A if(A[min] >A[j]) - { -Min =J; the } - } - - if(min! =i) + { -temp =A[min]; +A[min] =A[i]; AA[i] =temp; at } - } - - for(M =0; M <len; m++) - { -printf"%d", A[m]); in } - to return 0; + -}
Here, for example, in ascending order
Sort the array in ascending order, as shown in
The principle of choosing a sort is very simple, which is to always save the lowest element subscript of the currently unsorted sequence with a variable min. After the first loop of the outer layer is finished, compare whether the subscript that participates in the cycle is consistent with the current min, and if so, whether or not the position of the two subscript corresponding element is exchanged if it is inconsistent.
The following is a combination of this sentence to do an analysis:
First trip: Before comparing, define the subscript that Min uses to hold the current smallest element.
A[min] respectively and A[i+1] (A[j]) comparison, the first a[0](5) and A[1](7) comparison, a[0] small, continue, J + +, then a[0] and a[2] compared, a[0] large, then the current J value assigned to Min, That is min = 2; Because Min holds the current minimum element subscript, it is now found that someone younger than him, it is necessary to update their own values. So now is used a[2] and a[3] compared, a[2] small, continue J + +. A[2] and a[4] than, a[4] small. The value for update min is 4,j++. Continue to use a[4] and a[5] than, a[4] small, j + +. Always compare a[8], found are a[4] small, then the first trip compared to the end. The subscript for the smallest element is 4.
Then with the outer layer for the first time into the cycle of the I value and min comparison, if not equal, indicating that I corresponds to the element is not the smallest element, then the min subscript corresponding to the element interchange position, the smallest element is placed in the first array.
Second trip: At this time I is equal to 2, Min also re-initialized to 2, continue to let A[min] and a[i+1] comparison, the steps and the first trip is exactly the same, this trip down, you can find the smallest element of the corner mark.
The next step is to repeat the process.
Specific processes such as:
Second, Direct Insert Sort
Post Code First
1#include <stdio.h>2 intMain ()3 {4 inta[5] = {5,7,4,6,3};5 intLen =sizeof(a)/sizeof(int);6 inti,j,temp,m;7 for(i =1; i < Len; i++)8 {9 if(A[i] < a[i-1])Ten { Onetemp =A[i]; A - for(j = i1; J >=0&& A[j] > temp; j--) - { thea[j+1] =A[j]; - } -a[j+1] =temp; - } + } - for(M =0; M < Len; m++) + { Aprintf"%d", A[m]); at } - return 0; -}
The words are in the picture:
Third, bubble sort
Post code:
1#include <stdio.h>2 intMain ()3 {4 inta[5] = {5,6,3,7,4};5 inti,j,k,temp;6 intLen =sizeof(a)/sizeof(int);7 8 for(i =0; I < Len-1; i++)9 {Ten BOOLFlag =true; One for(j =0; J < Len-i-1; J + +) A { - if(A[j] > a[j+1]) - { thetemp =A[j]; -A[J] = a[j+1]; -a[j+1] =temp; -Flag =false; + } - + } A if(flag) at { - Break; - } - } - - for(k =0; K < Len; k++) in { -printf"%d", A[k]); to } + return 0; -}
:
, for the array elements for the 5,6,3,7,4 bubble sort, each comparison of adjacent two elements, if the former greater than the latter, then the interchange, conversely, do not change, continue to push forward. However, it can be found that after three bubbles, the final result has been produced: 3,4,5,6,7. But the program does not stop, it will continue to execute two rounds, the last two times bubbling, but this is meaningless. So in the code to add Flags flag setting, if there is no comparison, no longer perform the operation, you can think that the current sequence has been ordered in accordance with the requirements of the sequence is complete!
Summarize:
Simple selection Sort:
The basic idea is that each trip selects the smallest (or largest) element of the data element to be sorted as the first element of the array, and the simple selection of sorting is an unstable sort until all the elements have been exhausted . The time complexity is O (n2).
Direct Insert Sort:
The basic idea for each step is to insert a record to be sorted into an ordered sequence that has been ordered in the previous order until all elements have been inserted. The time complexity is O (N2).
Bubble Sort:
The basic idea is to make a 22 comparison of the adjacent elements, in reverse order, so that each trip will "float" the smallest or largest element to the top, eventually achieving complete order. The time complexity remains O (n2)
Comparison-based sorting---selection, insertion, bubbling