CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡
Algorithm ¡Analysis ¡and ¡Complexity ¡
1 ¡
CS 162 Intro to Programming II Algorithm Analysis and - - PowerPoint PPT Presentation
CS 162 Intro to Programming II Algorithm Analysis and Complexity 1 Topics How long does it run? Why measuring Ame wont work
1 ¡
The ¡"change" ¡in ¡T ¡is ¡constant, ¡i.e., ¡T ¡does ¡not ¡depend ¡on ¡the ¡size ¡of ¡n. ¡
int n = 100; int table[][] = new int[n][n]; for(int row = 0; row < n; row++) for(int col = 0; col < n; col++) table[row][col] = (row+1) * (col+1);
int n = 40; for (int row = 1; row <= n; row++){ for (int col = 1; col <= row; col++) System.out.print('*'); System.out.println(""); }
int n = 1000; while (n > 0){ System.out.println(n); n /= 2; }
n ¡ log ¡n ¡ n2 ¡ 1 ¡ 0 ¡ 1 ¡ 2 ¡ 1 ¡ 4 ¡ 3 ¡ 1.58 ¡ 9 ¡ 4 ¡ 2 ¡ 16 ¡ 5 ¡ 2.32 ¡ 25 ¡ 10 ¡ 3.32 ¡ 100 ¡ 100 ¡ 6.64 ¡ 10,000 ¡ 1000 ¡ 9.97 ¡ 1,000,000 ¡ 1,000,000 ¡ 19.93 ¡ 1012 ¡ If ¡a ¡single ¡step ¡takes ¡1 ¡ms, ¡the ¡log ¡n ¡algorithm ¡will ¡take ¡less ¡than ¡.02 ¡ seconds ¡for ¡n=1,000,000, ¡whereas ¡the ¡n2 ¡algorithm ¡will ¡take ¡over ¡31 ¡ years! ¡