2.1 Fundamentals of Mathematics
1. Mastering the concept of O (N)
2. In any analysis that requires a large o representation, various simplifications are possible, and low-order items are usually automatically ignored and constants can be discarded
2.2 Models
1. Assumptions made about the simulator:
1. The simulator does any simple work (addition, subtraction, assignment, comparison) that takes exactly one time unit
2. The simulator has unlimited memory, no fault pages will be interrupted
2.3 Issues to analyze
If no correlation is specified, the required amount is the worst-case run time
Example: Analysis of time complexity of three solutions for maximal subsequence and problem
In general, data read-in is a bottleneck, as long as possible, so that the algorithm is effective enough and does not cause the bottleneck of the problem is very important
2.4 Calculation of the run time
1. General rules:
Rule 1:for Loop once for loop time is at most the number of times the run time of the FOR Loop statement (including the test) is multiplied by the iteration
Rule 2: A statement of nested for loop nesting loops The total run time is multiplied by the run time of the statement by the size of all for loops
Rule 3: Sequential statement run time summation (in fact the maximum value is the elapsed time)
The rule 4:if/else statement never exceeds the total run time of the elders in addition to the S1 and S2.
2. Four ways to solve coding of maximum subsequence summation problem
1. O (n^3), 7105ns
Public Static intMaxsum (int[] list,intN) {intthissum, Maxsum, i,j,k; Maxsum= 0; for(i = 0; i < N; i++) { for(j = i; J < N; J + +) {thissum= 0; for(k = 0; k <=j; k++) {thissum+=List[k]; } if(Thissum >maxsum) {Maxsum=thissum; } } } returnmaxsum; }
2. O (n^2), 3158ns, as you can see, there's a layer of loops missing.
Public Static intMAXSUM2 (int[] list,intN) {intthissum, Maxsum, i,j; Maxsum= 0; for(i = 0; i < N; i++) {thissum= 0; for(j = i; J < N; J + +) {thissum+=List[j]; if(Thissum >maxsum) {Maxsum=thissum; } } } returnmaxsum; }
3. O (Nlogn), 22501ns, the data volume comparison is relatively small case, recursive comparison waste time
Public Static intMAXSUM3 (int[] list,intLeftintRight ) { intmaxleftsum,maxrightsum; intmaxleftbordersum,maxrightbordersum; intleftbordersum,rightbordersum; intMid,i; if(left = =Right ) { if(list[left]>0) { returnList[left]; } return0; } Mid= (left + right)/2; Maxleftsum=maxSum3 (list, left, mid); Maxrightsum= MAXSUM3 (list, Mid + 1, right); Maxleftbordersum= 0; Leftbordersum= 0; for(i = mid; I >= left; i--) {leftbordersum+=List[i]; if(Leftbordersum >maxleftbordersum) {Maxleftbordersum=leftbordersum; }} maxrightbordersum= 0; Rightbordersum= 0; for(i = mid + 1; I <= right; i++) {rightbordersum+=List[i]; if(Rightbordersum >maxrightbordersum) {Maxrightbordersum=rightbordersum; } } returnMath.max (Maxleftsum, Math.max (maxrightsum, Maxleftbordersum +maxrightbordersum)); }
4. O (N), 3158ns
Public Static intMAXSUM4 (int[] list,intN) {intThissum = 0; intMaxsum = 0; for(inti = 0; i < N; i++) {thissum+=List[i]; if(Thissum >maxsum) {Maxsum=thissum; } Else if(Thissum < 0) {thissum= 0; } } returnmaxsum; }
Data structure and algorithm Analysis-2nd chapter-Algorithm Analysis