Uploaded by Athena Lepenski

Final Exam Cheat Sheet-- Object Oriented Coding

advertisement
1.
2.
O(1) - Constant: The algorithm's execution time is independent of the input size, always takes the same amount of time to complete.
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
Linked / Singly Linked Lists
 A data structure consisting of a sequence of nodes, where each node contains data and a reference to the next node in the sequence
 A singly linked list is a specific type of linked list, where each node has a reference to the next node in the sequence, but not to the previous node. This means that it is
only possible to traverse the list in one direction, from the beginning to the end.
 In summary, the main difference between linked lists and singly linked lists is that linked lists are a more general data structure that can have different types of
references between nodes, while singly linked lists specifically have references to the next node only.
Compiler vs. Runtime Error
 Compiler errors occur during the compilation phase, when the code is translated into machine code. Compiler errors are related to syntax errors, type mismatches,
missing dependencies, or other issues that prevent the code from being compiled correctly.
 Runtime errors occur while the program is running. These errors are related to issues like dividing by zero, accessing an out-of-bounds array index, using an
uninitialized variable, or other issues that can't be caught by the compiler. These errors typically cause the program to crash or produce unexpected behavior.
Rounding: Integer division: Discards the decimal part of the result and returns only the integer part…. Regular Integers:
5.8  6
-5.8  -6
5.2  5
-5.2  -5
•
•
•
•
•
Good designs have:
•
High coHesion
•
A class should represent a single concept
•
Low coupling
•
When one object depends strongly on another
Low cohesion means that you have a small number of really large classes that do too much stuff (i.e., do more than one thing)
High coupling means you have many classes that depend (“know”) too much on each other
Data Classes often violate a principle of OOD called encapsulation because they aren’t in control of their own data – they are just dumb repositories for other classes to
use
("dumb" means few methods, e.g., just a getters and setters)
Encapsulation
•
Grouping related stuff together (i.e., capsule)
•
Each class has ONE JOB!
•
Rather than passing around data, pass around object instances (created from a class) that:
•
Provide a powerful set of methods that work on the data stored inside the object
•
These methods protect the private data stored inside the object from being used incorrectly
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
Download