The feature of this series of blogs is--give the results of each order
Originally thought to write a good process, get a picture of what, but think the online analysis too much, are better. "Introduction to the algorithm" above that analogy, than do playing cards inserted new cards, it is more image. So these blogs are even a summary of their own.
#include <stdio.h> void insertsort (int *m, int n) { int i,j,temp; for (i=1;i<n;i++) { temp=m[i]; j=i-1; while (j>=0 && temp<m[j]) { m[j+1]=m[j]; j--; } M[j+1]=temp; for (int t=0;t<10;t++) printf ("%d", m[t]); printf ("\ n");} } int main () { int i; int a[10]={2,8,5,7,4,3,1,9,6,10}; for (i=0;i<10;i++) printf ("%d", A[i]); printf ("\ n" \ n "); Insertsort (a,10); for (i=0;i<10;i++) printf ("%d", A[i]); printf ("\ n"); return 0;}
Wait.... It's not OVER!!!
The last 3 lines are in order, but they still don't stop.
Improve the sort algorithm
void Insertsort (int *m, int n) { int i,j,temp; BOOL Flag; for (i=1;i<n;i++) { temp=m[i]; j=i-1; Flag=false; while (j>=0 && temp<m[j]) { flag=true; M[J+1]=M[J]; j--; } M[j+1]=temp; for (int t=0;t<10;t++) printf ("%d", m[t]); printf ("\ n"); if (flag) break ; }}
The result is wrong, since the direct insertion of the sorting process, there may be one or two trips and no value exchanged. This is different from bubbling sort!
Therefore, to optimize the algorithm, it is necessary to meet the actual situation for analysis.
Sorting algorithm Big Collection _ Insert class--Direct insert Sort