Algorithms Flashcards
How to check if an algorithm has a specific complexity?
Difference between big O, theta, and omega notation?
big O describes the worst-case performance of an algorithm, it may perform better.
big Theta describes the specifically determined performance of an algorithm (down to constant level). E.g. an algorithm with runtime n^2 will always take a constant multiple of n^2 to run.
big Omega describes the best-case performance of an algorithm, it may perform worse.
What is a stable sorting algorithm?
If two elements are in a specific order before sorting and have equal values, they will be in the same order afterwards.
Examples are: merge sort, insertion sort and bubble sort.
What is the best case runtime for any comparison-based sorting algorithm?
nlogn
What does it mean that big Theta is symmetrical?
If f(n) is O(g(n)) then g(n) is O(f(n))
Which sorting algorithms are divide-and-conquer?
Quick sort, merge sort, heap sort, binary-tree sort
What is counting sort?
For an array with elements with a known maximum value k:
Create a secondary array B with k+1 positions.
Count occurrences of each number in its corresponding place in B.
Replace each number in B with the sum of the numbers before it.
Iterate over original array A, for each value look up the new index in C by indexing into B with the value.
After each insertion, decrement the value in B for that value.
It has runtime O(n).
Constructing a heap
Each node in the heap must be bigger than its children. To heapify: traverse the tree from the right side of the array to the left. If node does not fulfil heap property: heapify it.
Properties of heap sort?
heapify has runtime O(log n) and heap construction has runtime O(n log n).
Unstable.
In-place.
Properties of insertion sort?
In-place.
Stable.
O(n^2)
How to do radix sort?
Use an in-place sorting algorithm to sort the array of numbers by each digit individually, starting with the least-significant digit.
Sorts in O(d*f) where f is the runtime of the in-place sorting algorithm, and d is the number of digits in each element.