Intro Analyzing Time Complexity Overview n n n


![Insertion Sort Input: Array A[1. . n] Output: Sorted Array A[1. . n] for Insertion Sort Input: Array A[1. . n] Output: Sorted Array A[1. . n] for](https://slidetodoc.com/presentation_image_h2/5e5339b5de65febab40cb724716b4312/image-3.jpg)


![Best and Worst Cases n n Best-case: t [ j ] = 1, 2 Best and Worst Cases n n Best-case: t [ j ] = 1, 2](https://slidetodoc.com/presentation_image_h2/5e5339b5de65febab40cb724716b4312/image-6.jpg)






- Slides: 12
Intro: Analyzing Time Complexity
Overview n n n n Look at insertion sort example Count the number of operations Best-case and worst-case analysis Intro. to Big-Oh analysis Function definitions Rules of thumb for analysis Sample problems Experimental Complexity
Insertion Sort Input: Array A[1. . n] Output: Sorted Array A[1. . n] for (index=2; index<=n; index++) move A[index] to its correct position in subarray A[1. . index] 13 11 23 14 91 54 6 22 A. 13 B. 11 13 C. 11 13 23 D. 11 13 14 23 … etc.
Insertion Algorithm 1. for (j = 2; j <=length(A); j++) { 2. key = A[ j ]; 3. ind = j - 1; 4. /* move up larger ones */ 5. while (ind > 0 && A[ind ] > key) { 6. A[ ind + 1] = A[ ind ]; 7. ind = ind - 1; } 8. A[ ind + 1 ] = key; }
Compute Running Time
Best and Worst Cases n n Best-case: t [ j ] = 1, 2 j n T(n) = (c 1+c 2+c 3+c 5+c 8)n - (c 2+c 3+c 5+c 8) Worst-case: t [ j ] = j, 2 j n T(n) = (c 5+c 6+c 7)/2)n^2 +(c 1+c 2+c 3+c 5/2 -c 6/2 -c 7/2 +c 8)n - (c 2+c 3+c 5+c 8)
Big-Oh Analysis n n Get rid of detail Look at the growth rateu as n increases, u how much does time increase? n n Consider the leading term only Ignore the constant(s)
Definitions O-notation (upper bound) O(g(n)) = {f(n): 0 f(n) c*g(n)} n -notation (lower bound) (g(n)) = {f(n): 0 c*g(n) f(n)} n -notation (tight bound) (g(n)) = {f(n): 0 c 1*g(n) f(n), f(n) c 2*g(n)} there exists constants, c, c 1, c 2, n 0 n
Using the Definitions Suppose growth rate O(f(n)) New. Time = f(New. Size) * Old. Time ---------------f(Old. Size)
Rules of Thumb n n Loops: at most: Multiply the number of iterations of the loop by the running time inside Nested Loops: at most: Work inside out. Multiply the running time inside by the product of the iterations of all the loops. (over-estimate? )
Rules of the Thumb (2) Consecutive Statements: add the complexities (take the maximum) n n If-then statements: (at most) running time of the test plus the largest of the running times of the condition blocks
Class Exercises and Homework n n 1. Read Chapter 1 -2, through 2. 2 2. Fill in some of the squares in the chart on problem 1 -1: (pg. 15, or pg 13) Read 2. 1. “get” loop invariant Do 2. 2 -1, 2. 2 -2, 2. 2 -3 (pg 29) 3. Read section 2. 3 Next Time: Complexity of Recursive Algs. u Recursion analysis, (Sect. 2. 3) u More Big-Oh Examples u