week 3 wednesday what did we talk about last time project
play

Week 3 -Wednesday What did we talk about last time? Project 1 More - PowerPoint PPT Presentation

Week 3 -Wednesday What did we talk about last time? Project 1 More on Big Oh notation O establishes an upper bound f ( n ) is O ( g ( n )) if there exist positive numbers c and N such that f ( n ) cg ( n ) for all n N


  1. Week 3 -Wednesday

  2.  What did we talk about last time?  Project 1  More on Big Oh notation

  3.  O establishes an upper bound  f ( n ) is O ( g ( n )) if there exist positive numbers c and N such that f ( n ) ≤ cg ( n ) for all n ≥ N  Ω establishes a lower bound  f ( n ) is Ω ( g ( n )) if there exist positive numbers c and N such that f ( n ) ≥ cg ( n ) for all n ≥ N  Θ establishes a tight bound  f ( n ) is Θ ( g ( n )) if there exist positive numbers c 1 , c 2 and N such that c 1 g ( n ) ≤ f ( n ) ≤ c 2 g ( n ) for all n ≥ N

  4.  Implement binary search  How much time does a binary search take at most?  What about at least?  What about on average, assuming that the value is in the list?

  5.  Give a tight bound for n 1.1 + n log n  Give a tight bound for 2 n + a where a is a constant  Give functions f 1 and f 2 such that f 1 ( n ) and f 2 ( n ) are O( g ( n )) but f 1 ( n ) is not O( f 2 ( n ))

  6. int count = 0; for( int i = 0; i < n; i += 2 ) for( int j = 0; j < n; j += 3 ) count++;

  7. int count = 0; for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) { if(j == n - 1) i = n; count++; }

  8.  An interface is a set of methods which a class must have  Implementing an interface means making a promise to define each of the listed methods  It can do what it wants inside the body of each method, but it must have them to compile  A class can implement as many interfaces as it wants

  9.  An interface looks a lot like a class, but all its methods are empty  In Java 8 and higher, default implementations can be given, but never mind that now  Interfaces have no members except for ( static final ) constants public interface Guitarist { void strumChord(Chord chord); void playMelody(Melody notes); }

  10. public class RockGuitarist extends RockMusician implements Guitarist { public void strumChord( Chord chord ) { System.out.print("Totally wails on that " + chord.getName() + " chord!"); } public void playMelody( Melody notes ) { System.out.print("Burns through the notes " + notes.toString() + " like Jimmy Page!" ); } }

  11.  A class has an is-a relationship with interfaces it implements, just like a superclass it extends  Code that specifies a particular interface can use any class that implements it public static void perform(Guitarist guitarist, Chord chord, Melody notes) { System.out.println("Give it up " + "for the next guitarist!"); guitarist.strumChord( chord ); guitarist.playMelody( notes ); }

  12.  From a formal perspective, a type is a set of data values and the operations you can perform on them Type Values Operations Integers from -2147483648 to int + , - , * , / , % , << , >> , >>> , | , & 2147483647 double + , - , * , / , % Floating points numbers All possible Java String objects + , length() , charAt() , String substring() , etc. Wombat All possible Wombat objects toString() , eat() , etc.

  13.  So, you have a type with operations  Do you need to know how those operations are implemented to be able to use them?  No!  In fact, in OOP (including Java), the data is usually hidden from you  Enter the Abstract Data Type ( ADT )  It does something  We aren't necessarily concerned with implementation

  14.  The idea of an interface has a strong connection to an ADT  Let's look at the List<E> interface  Some of its methods:  boolean add(E element)  void add(int index, E element)  void clear()  E get(int index)  int size()  boolean remove(Object o)

  15.  There are lots of different ways of keeping a list of data  The List ADT doesn't care how we do it  And there are lots of implementations that Java provides:  ArrayList  LinkedList  Stack  Vector  You can use whichever you think best suits your task in terms of efficiency

  16.  A bag is an ADT that is iterable but otherwise only has one real operation  Add  Put an element in the bag  It's a collection of things in no particular order  A bag is also called a multiset  The book talks about bags partly because it's hard to imagine a simpler ADT

  17.  The list ADT is not entirely standardized  Some lists allow insertion at the beginning, end, or at arbitrary locations  Some lists allow elements to be retrieved from an arbitrary location  Let's focus on an list that allows the following operations  Add  Insert element at the end of the list  Add at index  Insert element at an arbitrary location  Get  Retrieve element from arbitrary location

  18.  A stack is an ADT with three main operations  Push  Add an item to the top of the stack  Pop  Remove an item from the top of the stack  Top  Retrieve the item at the top of the stack  Stacks are often implemented with a dynamic array or a linked list

  19.  A queue is an ADT with three main operations  Enqueue  Add an item to the back of the queue  Dequeue  Remove an item from the front of the queue  Front  Retrieve the item at the front of the queue  Queues are also often implemented with a dynamic array or a linked list

  20.  A stack is a simple (but useful) ADT that has three basic operations:  Push Put an item on the top of the stack  Pop Remove an item from the top of the stack  Top Return the item currently on the top of the stack (sometimes called peek )

  21.  When are stacks used?  Implicitly, in recursion (or in any function calls)  Explicitly, when turning recursive solutions into iterative solutions  When parsing programming languages  When converting infix to postfix

  22.  Advantages:  Pop is Θ (1)  Top is Θ (1)  Disadvantages  Push is Θ ( n ) in the very worst case, but not in the amortized case

  23. public class ArrayStack { private int[] data; private int size; public ArrayStack() {} public void push(int value) {} public int pop() {} public int peek() {} //instead of top public int size() {} }

  24.  Finish stacks  Queues

  25.  Read section 1.3  Finish Assignment 1

Recommend


More recommend