Final Exam Cheat Sheet - Object Oriented Coding
Final Exam Cheat Sheet - Object Oriented Coding
O(1) - Constant: The algorithm's execution time is independent of the input size, always takes the same amount of time to complete.
2. O(log n) - Logarithmic: The algorithm's execution time grows logarithmically with the input size. This means that as the input size increases, the execution time
increases much more slowly than the input size.
3. O(n) - Linear: The algorithm's execution time is proportional to input size, as the size increases, the execution time increases linearly.
4. O(n log n) - Linearithmic: The algorithm's execution time grows faster than linear time, but slower than quadratic time. This is often seen in sorting algorithms like
merge sort and quicksort.
5. O(n^2) - Quadratic: The algorithm's execution time is proportional to the square of the input size. This means that as the input size increases, the execution time
increases exponentially.
6. O(2^n) - Exponential: The algorithm's execution time doubles with every addition to the input size, extremely slow for large inputs.
Big-O describes the worst-case of an algorithm's performance-- May perform better on average or for specific input sizes, compares the efficiency of different algorithms
Binary search:
o Worst-case runtime: O(log n)
o Best-case runtime: O(1)
Insertion sort:
o A sorting algorithm that iterates over an unsorted list, comparing each element with the previously sorted elements and inserting it into the correct position in
the sorted list
o Worst-case runtime: O(n^2)
o Best-case runtime: O(n)
Selection sort:
o A sorting algorithm that selects the minimum element from an unsorted list and places it at the beginning of the list, repeating this process until the entire list
is sorted
o Worst-case runtime: O(n^2)
o Best-case runtime: O(n^2)
Merge sort:
o A divide-and-conquer sorting algorithm that recursively splits an unsorted list into sublists, sorts the sublists, and then merges the sorted sublists to produce
the final sorted list
o Worst-case runtime: O(n log n)
o Best-case runtime: O(n log n)
Adding or deleting / inserting node at an index position:
o Worst-case runtime: O(n) - n is the number of nodes, occurs when index position to add new element is at the end of the list
o Best-case runtime: O(1) - when adding the new element at the beginning of the list
Searching for an element in a LinkedList (with or without a tail pointer):
o Worst case: O(n) - finding the target element at the end of the list or not finding it and having to traverse the entire list
o Best case: O(1) - finding the target element at the beginning of the list
For a for / while loop (depends on the number of iterations in the loop):
o Best case: O(1) - If the loop has a fixed number of iterations that does not depend on the input size
o Worst case: O(n) - If the loop has n iterations that depend on the input size, linear in the input size n
Adding or deleting an element to an ArrayList:
o Worst case: O(n) - adding element at the beginning or middle of the list, which requires shifting all subsequent elements
o Best case: O(1) - adding an element to the end of the list
Determining Big-O
Identify the basic operations
Count the number of times each operation is performed
Express the runtime in terms of the input size
Simplify the expression by removing lower-order terms and constants
Identify the Big-O runtime based on the dominant term
Cannot instantiate an abstract class! (ie. A var7 = new A();, when A is abstract) – Compiler Error
Cannot typecast or instantiate up the chain (ie. C var8 = new B(); OR ((D)var6).method1();)—Compiler Error
this.refers to the instantiated type (ie. B var5 = new C(); this.method2() refers to C’s method2())
super.method refers to the method one level above where it is being called (ie. C’s super.method3() refers to B’s method3())
Must check if the declared type has the method first!
HashMap:
put(key, value): adds the key-value pair to the map
get(key): retrieves the value associated with the key
remove(key): removes the key-value pair from the map
The time complexity for put(), get(), and remove() operations is O(1) on average
Singly Linked List:
Each element, called a node, contains a value and a reference to the next node
The first node is called the head and the last node is called the tail, the tail's reference is null
Basic operations for SLL include:
insert(value): adds a new node to the end of the list
remove(value): removes the first node with the given value from the list
get(index): retrieves the value of the node at the given index
size(): returns the number of nodes in the list
The time complexity for insert() and remove() operations is O(1) on average, the time complexity for get() operation is O(n) in the worst case