Download Big O & ArrayList and more Summaries Java Programming in PDF only on Docsity!
Big O & ArrayList
15 - 121 Fall 2020
Margaret Reid-Miller
Today
- Office Hours: Thursday afternoon (time to be
announce on Piazza)
- Big O
- Amortized runtime
- ArrayLists
Big O: Formal Definition
- Let T(n) – the number of operations performed in an
algorithm as a function of n.
- T(n) ∈ O(f(n)) if and only if there exists two constants,
n 0 > 0 and c > 0 and a function f(n) such that for all
n > n 0 , cf(n) ≥ T(n).
How long does it take to
- say "California" and then
- fly to California
The time to fly to California (roughly 6 hours)
dominates the time to say "California", so we
ignore the time to say "California".
More on Big O
- Big O gives us an upper-bound approximation on the
complexity of a computation.
- That is, think of Big O as “<=”
- n + 1000 is O(n), but it’s also O(n 2
) and O(n
3
). We
try to keep the bound as tight as possible, though,
so we say n + 1000 is O(n).
Big-O when algorithm is A then B
- Suppose an algorithm is do
A followed by B.
- Then the overall complexity of the algorithm is
max (O(A), O(B)).
Examples:
O(log n) + O(n) =
O(n log n) + O(n) =
O(n log n) + O(n
2
O(
n
) + O(n
2
O(n)
O(n log n)
O(n
2
O(
n
How do we grow the contacts array when it is full?
We create a new array that is larger than the contacts
array and copy all the elements to the new array.
Sometimes, the cost to add a single Person is O(1)
because there is room in contacts.
But sometime the cost to add a single person is O(n),
n = numContacts, because we need to expand the
array and copy n elements.
What is the worst case runtime for calling
add(name, number) n times, when we start with an
array of length 1?
Number of copies to grow an array to length
n starting with an array of length 1.
Grow by 1 each time:
The array is full when 1, 2, 3, 4, 5, 6, … elements in the array After adding n elements we copied 1 + 2 + 3 + 4 + …(n-1) = n(n-1)/2 = O(n 2 ) elements
Grow by 100 each time:
The array is full when 100, 200, 300, 400, 500, … elements in the array After we have added n = 100*k elements we copied 100 + 200 + 300 + … + 100(k-1) elements = 100k (k-1)/ = n (n/100 – 1)/2 = O(n 2 )
Growing by a constant
does O(n
2
) copies
What is the worst-case run time for adding n Persons to a ContactList?
Therefore, the worst-case runtime for n calls to
add() is O(n).
We, therefore, say that the amortized worst-case
runtime for a single call to add() is O(1).
Definition: Amortized worst-case runtime is the
expected runtime per operation of a worst-case
sequence of n operations.
(This is not the same as Average runtime , which
is runtime of a single operation averaged over
all distinct inputs .)
ArrayLists
The ArrayList Class
- For Java folks, an ArrayList is like an array, but:
- It’s a class, so we construct it and call methods on it.
- It’s resizable. It grows as we add elements and shrinks
as we remove them.
- For Python folks, an ArrayList is like a Python list, but:
- We do not use subscript notation.
- ArrayLists (like arrays) are homogeneous. ArrayList names = new ArrayList();
ArrayList methods
boolean add (E obj) Appends obj to end of this list; returns true void add (int index, E obj) (0 <= index <= size) Inserts obj at position index void clear () Removes all the elements from this list. boolean contains (Object obj) Returns true if this list contains obj. E get (int index) Returns the element at position index (0 <= index < size) int indexOf (Object obj) Returns the index of the first occurrence of obj in this list, or returns - 1 if this list does not contain obj java.util.ArrayList
ArrayList complexity
int size() add(E obj) add(int index, E obj) get(int index) set(int index, E obj) contains(Object obj) remove(int index) remove(Object obj) indexOf(Object obj) clear() isEmpty() Worst Best O(1) O(1)* O(n) O(1)* O(1) O(1) O(n) O(1) O(n) O(1) O(n) O(n) O(1) O(1) O(1) *amortized
ArrayList demo
import java.util.ArrayList; ArrayList names = new ArrayList(); names.add(“Margaret”); names.add(“Dave ” ); names.get(1); names.set(0, “Mark”); names.add(1, “Tom”); names.remove(1); // Margaret // Margaret Dave // returns Dave // Mark Dave // Mark Tom Dave // Mark Dave