Bought an introduction to the algorithm, the use of idle time, learning algorithm analysis. Language uses C + +, by the way practice C + + language features.
Insert Sort: This is an efficient algorithm for ordering a small number of elements. The following is an example to analyze the principle of sorting it.
For a vector:
vector<int2324123 4 };
First on the code:
for(inti =1; I < numbers.size (); ++i) {key=Numbers[i]; J= i-1; while(J >=0&& Numbers[j] <key) {Numbers[j+1] =Numbers[j]; J-=1; } numbers[j+1] =key; for(Auto &it:numbers) cout<< it <<"\ t"; cout<<Endl; }
The variable i represents the element that currently needs to be inserted, and when I iterate from 1 to N-1 (n for the total number of elements), there are always numbers No. 0 to i-1 elements that are ordered sequentially.
The above code will display the results of each iteration to facilitate understanding of the principle of the algorithm.
Algorithm Analysis (1): Insert sort