Complexity BigO notation describes the behavior of an
Complexity Big-O notation describes the behavior of an algorithm as the input size becomes large (infinite) the behavior is typically running time or memory usage in ascending amount of running time: 1 O(1) constant O(logn) logarithmic O(n) linear O(n 2) quadratic O(n 3) cubic. . . (higher order polynomials) O(2 n) exponential
O(1) means the algorithm takes a constant number of operations to complete (i. e. independent of input size) 2
O(n) means the run time of the algorithm grows in proportion to the input size 3
how can you show that the algorithm is O(n)? count the number of operations each for loop is turned into a summation the number of operations inside the loop body is what you sum for(int i = 0; i < n; ++i) { s = s + array[i]; } 4
Loop Invariants a loop invariant is: a relationship between program variables involved in a loop this includes the index variable(s) in a for loop true before the loop begins true at the bottom of the loop // Loop Invariant is true here for(int i = 0; i < n; ++i) { /* body of loop here */ // ++i // Loop Invariant is true here } 5
briefly mentioned in Notes Chap 5 fundamental to proofs of correctness (for loops) /** @pre. array. length > 0 */ public static int sum. Of(int[] array) { int s = array[0]; // Loop Invariant is true after i is initialized for(int i = 1; i < array. length; ++i) { s = s + a[i]; // ++i // Loop Invariant is true here // s == a[0] + a[1] +. . . + a[i-1] // && 1 <= i <= array. length } } 6
/** @pre. array. length > 0 */ public static int min. Of(int[] array) { int m = array[0]; // Loop Invariant is true after i is initialized for(int i = 1; i < array. length; ++i) { if( a[i] < m ) { m = a[i]; } // ++i // Loop Invariant is true here // m is the minimum of { a[0], a[1], . . . , a[i-1] } // && 1 <= i <= array. length } } 7
O(n 2) or O(mn) slightly modified version of problem on practice test 3 given two arrays of int of length n write a method that counts the number of common elements 8 the elements in an array are unique e. g. [7, 4, 11, 19, 3] and [1, 8, 7, 11, 32] have 2 elements in common
/** @pre. a. length > 0 && b. length > 0 */ public static int count. Common(int[] a, int[] b) { int count = 0; for(int a. Int : a) { for(int b. Int : b) { if(a. Int == b. Int) { count++; } } } return count; } 9
GUIs we designed graphical user interfaces (GUIs) using Model-View-Controller (MVC) model view represents state of the application and the rules that govern access to and updates of state presents the user with a representation of the model state controller 10 processes and responds to events (such as user actions) from the view and translates them to model method calls
UML Diagram View is a subclass of JFrame View 1 Controller has 1 View 11 1 Model Controller has 1 Model
From Practice Test 2 A GUI application designed based on the MVC principle is made up of an app and three classes: View (it displays two buttons and a text field on a frame), Controller (which acts as a controller), and Model (based on an Array. List). In addition, the application involves classes from the Java library of which we will focus on four: JFrame, JButton, JText. Field, and Array. List. Hence, aside from the app, this application involves seven classes in total. Draw a UML class diagram that shows the relationships amongst these seven classes. For each, draw a rectangle containing the class name and then connect these rectangles to show inheritance and aggregation relationships. Note that the app is not included in the UML diagram. 12
JFrame View 1 Controller 1 Model 1 1 JText. Field 2 1 JButton Array. List remember that the Controller is the Action. Listener for the buttons 13
Model - text : String - is. Palidrome : boolean + Model() + set. Text(String) : void + is. Palindrome() : boolean - is. Palindrome(String) : void 14
View + GO : String - user. Input. Field : JText. Field - is. Palindrome. Field : JText. Field - go. Button : JButton + View(Model) + get. User. Input() : String + set. Is. Palindrome(boolean) : void 15
16
17
- Slides: 17