























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























3
2
n
2
2
n
How do we grow the contacts array when it is full?
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
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 )
2
What is the worst-case run time for adding n Persons to a ContactList?
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
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
import java.util.ArrayList; ArrayList