DS Flashcards
List 6 methods an ArrayList can perform?
add(e)
remove(pos)
get(post)
set(pos, e)
size()
clear()
Explain the time complexity of inserting into an array?
O(n) worst case because you have to go through all of the elements before the one you are inserting
Why do we use Big O notation?
Big O notation is used to describe the performance of an algorithm. It helps us understand scalability of an algorithm
What is O(1)?
Size of input doesn’t matter it will always run in constant time
What is O(n)?
The cost of the algorithm grows linearly with the size of the input.
We drop constants because they don’t matter because the constant is independent of N
What is O(N^2)?
The algorithm runs in quadratic time
What is O(log n)
logarithmic time complexity
O(log N) is more scalable and efficient than O(N) because it slows down at some point ie., Binary Search
What is O(2^n)
Not scalable at all (opposite of logarithmic growth rates)
What is an example of O(n) space complexity?
String[] a = new String[items.length]
What are issues with an Array?
Fixed size
What is the time complexity of inserting into an array?
O(1) first index
(worst case O(n) because we have to skip all the elements before that index)
What is the time complexity removing an item from an array?
O(n) worst case
How do you print an array?
System.out.println(Arrays.toString(nums));
How do you get the length of an array?
array.length
What are two types of dynamic arrays?
Vector
ArrayList