for solving a problem within a finite amount of time. • For example, finding the factorial of a number • 3! = 3 * 2 * 1 = 6 • 4! = 4 * 3 * 2 * 1 = 24 • 5! = 5 * 4 * 3 * 2 * 1 = 120
factorial algorithm to be general (able to solve any factorial problem), we need to write the algorithm in terms of n, where n is the number used as input to the factorial • n! = n * n-1 * n-2 * n-3 * …* 3 * 2 * 1
(error) 2. If n is 0, return 1 (mathematical definition of a factorial) 3. Initialize the product to 1 4. Loop from i = n to i = 1, count down by 1 each loop
of a particular algorithm? Count the number of repeated operations For example, the factorial program has a for loop, which loops n times, where n is the number that the user entered on the command line.
capital letter O to specify the upper bound of an algorithm For example, “Algorithm A has O(f(n))” or “A(n) = O(f(n))” means that “algorithm A has order f(n)”
a line for movie tickets, your waiting time is proportional to the number of people in the line So you will have to wait O(n), where n is the number of people in the line
loop in some way, then it is O(1) • Each of the instructions below is O(1) int n = 1000000000; //1 billion int x = n; int y = x * 10 + 2 / x – 3 % x; if(x < y){x++;} else{--y;} System.out.println(y);
loop in which the counter doubles, then this loop repeats O(log2 n) times int n = 1000000000; //1 billion for(int i=1;i<n;i=i*2){ System.out.println(i);}
we can still use O(log2 n) for any loop in which the counter is multiplied by a constant, so even if we multiply by 10 instead of 2, we still have O(log2 n) int n = 1000000000; //1 billion for(int i=1;i<n;i=i*10){ System.out.println(i);}
multiplication, loops that divide the counter by a constant are O(log2 n) int n = 1000000000; //1 billion for(int i=n;i>1;i=i/7){ System.out.println(i);} for(int i=n;i>1;i=i/17){ System.out.println(i);}
anyway? • log is short for logarithm • Take the base (b) of the log to the result (r) of the log to get number (n) • In other words, logs are directly related to exponents • r = logb n is the same as br = n
log2 32 = 5, so i = 1, 2, 4, 8, 16, 32 int n = 1000000000; //1 billion for(int i=1;i<32;i=i*2){ System.out.println(i);} • Note that this loop is O(1), because it is NOT dependent on n
in which the counter increments by 1 (one), then this loop repeats O(n) times int n = 1000000000; //1 billion for(int i=1;i<n;i++){ System.out.println(i);} • Note that i++ is the same as i=i+1
which the counter decrements by 1 (one), then this loop repeats O(n) times int n = 1000000000; //1 billion for(int i=n;i>0;i--){ System.out.println(i);} • Note that i-- is the same as i=i-1
subtract from a counter by a constant amount, then the loop repeats O(n) times int n = 1000000000; //1 billion for(int i=0;i<n;i=i+108){ System.out.println(i);}
we multiply the Big-O for both loops int n = 1000000000; //1 billion for(int i=1;i<n;i++){ System.out.println(i); for(int j=1;j<n;j=j*2){ System.out.println(j); }//inner loop: O(log2 n) }//outer loop: O(n)
two loops and still have O(n*log2 n) int n = 1000000000; //1 billion for(int i=1;i<n;i=i*2){ System.out.println(i); for(int j=1;j<n;j++){ System.out.println(j); }//inner loop: O(n) }//outer loop: O(log2 n)
rapidly with n Doubling n increases the run time by a factor of four (4) – Slower processing time than O(n*log2 n), but faster processing time than O(n3)
we multiply the Big-O for both loops int n = 1000000000; //1 billion for(int i=1;i<n;i++){ System.out.println(i); for(int j=1;j<n;j++){ System.out.println(j); }//inner loop: O(n) }//outer loop: O(n)
more rapidly with n Doubling n increases the run time by a factor of eight (8) Slower processing time than O(n2), but faster processing time than O(2n)
Later on in the class, we will show more typical examples of O(2n) int n = 1000000000; //1 billion for(int i=0;i<Math.pow(2,n);i++){ System.out.println(i);}
-1 (error) O(1) 2. If n is 0, return 1 (mathematical definition of a factorial) O(1) 3. Initialize the product to 1 O(1) 4. Loop from i = n to i = 1, count down by 1 each loop O(n)
the Big-O for each separate step, and multiply the Big-O for any steps within a loop step1 + step2 + step3 + (step4 * step5) + step6 O(1) + O(1) + O(1) + (O(n) * O(1)) + O(1) = O(n) * O(1) = O(n*1) = O(n)
• Method displayMenu() No looping, so O(1) • Method readFromFile() The while loop will loop one time for each line in the file, so O(n), where n is the number of lines in the file
O(1) • Method delete() In the worst case, the user will delete the first item in the list The for loop will loop from 1 to listSize, so O(n), where n is the size of the list (which is an array)
is used to display all of the contents in the array, so O(n), where n is the size of the array • Method writeToFile() The for loop is used to write all of the contents from the array to the file, so O(n), where n is the size of the array
• How to use Big-O to analyze time requirements of algorithms and methods • Big-O for factorial algorithm and factorial method • Big-O for the grocery list methods
1.Complete the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture