Index:
Open Source Spring Solution--lm.solution
See Code GitHub:
Sort.cpp
Code Brief Analysis Description:
1. For(int i=1;i<nsize;i++)
This outer for loop, [0][1],[1][2],[2][3] ... This order lets the inner layer follow the bad start index decrement comparison, guarantee each pair start
The inner layers are sorted before the bad sort.
2. While(Tmp<ary[n]) ... n--...
This inner layer while loop, so that the outer layers of the beginning of each loop, if the size of the comparison after the true, then transposition, n--to ensure that the n+1 is the index corresponding
Values can always be in the correct position
3. Operating Environment
Visual C + + IDE environment, it is recommended to actually run and debug a bit, the results are as follows:
4. Code:
1#include <iostream>2 3 using namespacestd;4 5 //defining functions--inserting sorting algorithms6 voidSort (int* ary,intnSize)7 {8 /*9 * Cyclic elements, dynamic understanding:Ten * arr[1] and arr[0] comparison One * arr[2] and arr[1],arr[0] comparison A * arr[3] and arr[2],arr[1],arr[0] comparison - * and so on and so on - */ the for(intI=1; i<nsize;i++) - { - inttmp=Ary[i]; - intn=i-1; + - //swap position If the latter element is less than the previous element + while(tmp<Ary[n]) A { atary[n+1]=Ary[n]; - - //element is dynamic decrement -n--; - - //after the first element, the element is not in if(n==-1) - { to Break; + } - } the *ary[n+1]=tmp; $ }Panax Notoginseng } - the intMainintargcChar*argv[]) + { A //arrays that need to be sorted the intnumarray[]={ A, to,5, the,1, About}; + intNlength=sizeof(Numarray)/sizeof(int); - $ //Sort $ Sort (numarray,nlength); - - //show the sorted results the for(intk=0; k<nlength;k++) - {Wuyicout<<numarray[k]<<","; the } -cout<<Endl; Wu - //Exit About return 0; $}
Mask
2018-06-12 21:50 Tuesday
C + +-insert Sort algorithm