SLIDE 1
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 - - 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
SLIDE 2
SLIDE 3
SLIDE 4
SLIDE 5
SLIDE 6
SLIDE 7
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 c1,c2 and N such that
c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ N
SLIDE 8
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?
SLIDE 9
Give a tight bound for n1.1 + n log n Give a tight bound for 2n + a where a is a constant Give functions f1 and f2 such that f1(n) and f2(n) are O(g(n)) but
f1(n) is not O(f2(n))
SLIDE 10
int count = 0; for( int i = 0; i < n; i += 2 ) for( int j = 0; j < n; j += 3 ) count++;
SLIDE 11
int count = 0; for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) { if(j == n - 1) i = n; count++; }
SLIDE 12
SLIDE 13
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
SLIDE 14
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); }
SLIDE 15
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!" ); } }
SLIDE 16
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 ); }
SLIDE 17
SLIDE 18
From a formal perspective, a type is a set of data values and
the operations you can perform on them
Type Values Operations int Integers from -2147483648 to 2147483647
+, -, *, /, %, <<, >>, >>>, |, &
double Floating points numbers
+, -, *, /, %
String All possible Java String objects +, length(), charAt(),
substring(), etc.
Wombat All possible Wombat objects
toString(), eat(), etc.
SLIDE 19
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
SLIDE 20
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)
SLIDE 21
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
- f efficiency
SLIDE 22
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
SLIDE 23
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
SLIDE 24
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
SLIDE 25
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
SLIDE 26
SLIDE 27
A stack is a simple (but useful) ADT that has three basic
- perations:
- 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)
SLIDE 28
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
SLIDE 29
SLIDE 30
Advantages:
- Pop is Θ(1)
- Top is Θ(1)
Disadvantages
- Push is Θ(n) in the very worst case, but not in the amortized case
SLIDE 31
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() {} }
SLIDE 32
SLIDE 33
SLIDE 34
SLIDE 35
SLIDE 36
SLIDE 37
SLIDE 38
SLIDE 39
Finish stacks Queues
SLIDE 40