Cheat Sheet

advertisement
Big O
Worst
Best
Sort
Selection
𝑂(𝑛2 )
𝑂(𝑛2 )
Inplace?
No
extra
space
Y
Stable
?
Same
key
value
N
Given array of n items
1. Find largest item
2. Swap it with item in the first index
3. Increase index by 1 and move down
array
Insertion
𝑂(𝑛2 )
𝑂(𝑛)
Y
Y
Insert an item into a properly sorted array
Bubble
w/o flag
𝑂(𝑛2 )
𝑂(𝑛2 )
Y
Y
Compare ith and (i+1)th item.
Bubble down the array
Add a Boolean to improve best case to 𝑂(𝑛)
Merge
𝑂(𝑛 log 𝑛)
𝑂(𝑛 log 𝑛)
N
Y
Divide-and-conquer method solves problem by
three steps:
Divide Step: divide larger problem
into smaller problems
(Recursively) solve the smaller
problems
- Conquer Step: combine results of
smaller problems to produce result of
larger problem
Additional space complexity of O(n)
Radix
Quick
𝑂(𝑛)
𝑂(𝑛)
N
Y
Treats each data to be sorted as a
character string. Organise the data into
groups according to the next character in
each data.
𝑂(𝑛 log 𝑛)
Y
N
𝑂(𝑛2 )
Use a partition and split the array into two
regions, a[i] >= p AND a[i] < p
Implementation
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int index = i;
for (int j = i+1; j < a.length; j++) {
if (a[j] > a[index])
index = j;
}
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
}
public static void insertionSort(int[] a) {
for (int i=1;i<a.length;i++) {
int next = a[i];
int j;
for (j=i-1; j>=0 && a[j]>next; j--)
a[j+1] = a[j];
a[j+1] = next;
}
}
public static void bubbleSort(int[] a) {
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < a.length - i; j++) {
if (a[j] > a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
public static void mergeSort(int[] a, int i, int j) {
if (i < j) {
int mid = (i+j)/2; // divide
mergeSort(a, i, mid); // recursion
mergeSort(a, mid+1, j);
merge(a,i,mid,j); //conquer: merge a[i..mid] and
//a[mid+1..j] back into a[i..j]
}
}
public static void merge(int[] a, int i, int mid, int j) {
// Merges 2 sorted sub-arrays a[i..mid] and a[mid+1..j] into one sorted sub-array a[i..j]
int[] temp = new int[j-i+1]; // temp storage
int left = i, right = mid+1, it = 0;
// it = next index to store merged item in temp[]
while (left<=mid && right<=j) { // output the smaller
if (a[left] <= a[right])
temp[it++] = a[left++];
else
temp[it++] = a[right++];
// Copy the remaining elements into temp.
while (left<=mid) temp[it++] = a[left++];
while (right<=j) temp[it++] = a[right++];
for (int k = 0; k < temp.length; k++)
a[i+k] = temp[k];
}
radixSort(int[] array, int n, int d) {
// Sorts n d-digit numeric strings in the array.
for (j = d down to 1) { // for digits in last position to 1st position
initialize 10 groups (queues) to empty
for (i=0 through n-1) {
k = jth digit of array[i]
place array[i] at the end of group k
}
Replace array with all items in group 0, followed by all items
group 1, and so on.
}
}
... quickSort(int[] a, int i, int j) {
if (i < j) { // Q: What if i >= j?
int pivotIdx = partition(a, i, j);
quickSort(a, i, pivotIdx-1);
quickSort(a, pivotIdx+1, j);
}
}
... partition(int[] a, int i, int j) {
// partition data items in a[i..j]
int p = a[i]; // p is the pivot, the ith item
int m = i;
// Initially S1 and S2 are empty
for (int k=i+1; k<=j; k++) { //process unknown region
if (a[k] < p) { // case 2: put a[k] to S1
m++;
swap(a,k,m);
} else {
// case 1: put a[k] to S2. Do nothing!
}
in
}
swap(a,i,m); // put the pivot at the right place
return m; // m is the pivot final position
}
O(1) < O(log 𝑛) < O(𝑛𝑐 ) < O(𝑛) < O(𝑛 log 𝑛) < O(𝑛2 ) < O(𝑛3 ) < O(𝑛!)O(2𝑛 ) log10 𝑛 =
log2 𝑛
log2 10
 Basically just count the number of statements executed.
 If there are only a small number of simple statements in a program
– O(1)
 If there is a ‘for’ loop dictated by a loop index that goes up to n
– O(n)
 If there is a nested ‘for’ loop with outer one controlled by n and the inner one controlled by m
– O(n*m)
 For a loop with a range of values n, and each iteration reduces the range by a fixed fraction (usually it is 0.5, i.e., half)
– O(log n)
 For a recursive method, each call is usually O(1). So
 if n calls are made – O(n)
 if n log n calls are made – O(n log n)
𝑆𝑢𝑚 𝑜𝑓 𝐴𝑃 =
𝑛
(2𝑎 + (𝑛 − 1)𝑑)
2
𝑆𝑢𝑚 𝑜𝑓 𝐺𝑃 =
Modifier
class
public
protected
No
modifier
private
𝑎(1 − 𝑟 𝑛 )
1−𝑟
Subclas
s
✔
✔
✘
World
✔
✔
✔
packag
e
✔
✔
✔
✔
✘
✘
✘
Division
Multiplication
String
Abstract class
Describes similar functionality & data for classes that
extend it
Can have implemented methods and attributes
Can have non-final data fields
Classes can only extend a single abstract class
Classes that extend an abstract class need not provide
implementation. They will be in turn abstract classes
class Planet extends Ball {
✔
✘
✘
ℎ𝑎𝑠ℎ(𝑘) = 𝑘%𝑚
ℎ𝑎𝑠ℎ(𝑘) = ⌊𝑚(𝑘𝑨 − [𝑘𝑨])⌋
𝑠𝑢𝑚 = 𝑠𝑢𝑚 ∗ 31 + 𝑐
Separate Chaining
Use a linked-list to connect collided
keys
Linear Probing
Move down until you find an empty
slot
Move down every quadratic per probe
Use a second hash function for probing
Quadratic Probing
Double Hashing
Interface
Describes similar functionality for all classes that
implements it
Only has method signatures and are only public
All data attributes must be final
Classes can implement multiple interfaces
classes that implement interface must provide
implementation for all methods defined in interface
class ComplexPolar implements Complex {
𝛼=
𝑛
, 𝑛 = 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑘𝑒𝑦𝑠
𝑚
𝑚 = ℎ𝑎𝑠ℎ 𝑡𝑎𝑏𝑙𝑒 𝑠𝑖𝑧𝑒 − 𝑛𝑜 𝑜𝑓 𝑠𝑙𝑜𝑡𝑠
𝛼 = 𝑙𝑜𝑎𝑑 𝑓𝑎𝑐𝑡𝑜𝑟
(ℎ𝑎𝑠ℎ(𝑘𝑒𝑦) + 1 ∗ 𝑑) % 𝑚
(ℎ𝑎𝑠ℎ(𝑘𝑒𝑦) + 𝑘 2 ) % 𝑚
(ℎ𝑎𝑠ℎ2 (𝑘𝑒𝑦) = 5 − (𝑘𝑒𝑦 % 5)
public void reverse() {
// Special case when head is null
if (head == null)
return;
ListNode<E> curr = head;
ListNode<E> next = null; // This will be correctly set later
ListNode<E> prev = null;
// While there is still more nodes
while (curr != null) {
// Remember the next node
next = curr.getNext();
// Reverse pointer for current node
curr.setNext(prev);
// Move on to the next node
prev = curr;
curr = next;
}
// Set the new head
head = prev;
}
public static void PowerSet(String finalString, String startString) {
// Base case. When there is only one character left in
// the starting string, print the finalString with and
// without that character.
if(startString.length() <= 1) {
System.out.println(finalString+startString);
System.out.println(finalString);
} else {
// Takes the first character out from the original
// string (startString), and recurse with cases
// where that character is both added and not
// added to the final string.
PowerSet(finalString + startString.charAt(0),
startString.substring(1));
PowerSet(finalString, startString.substring(1));
}
}
public static void PowerSet(String input) {
PowerSet("", input);
}
public static void main(String args[]) {
PowerSet("abc");
}
}
Hashtable
Hashmap
Hashtable(int initialCapacity) HashMap(int initialCapacity)
Hashtable(int intCapacity,
HashMap(int intCapacity,
float loadFactor)
float loadFactor)
put(K key, V value)
get(Object key)
isEmpty()
remove(Object key)
containsKey(Object key)
containsValue(Object value)
clear()
size()
clone()
Download