(W1) Divide and Conquer Flashcards

(28 cards)

1
Q

3x Big O Notations

A

Big O (upper bound)
Big Omega Ω (lower bound)
Big Theta Θ (exact)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to know if an algorithm is correct?

A
  1. Program always terminates
  2. Produces the correct result when it terminates
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Loop Invariants?

A

Conditions that hold true before and after each iteration of the loop - and possibly throughout the execution of the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the cost of comparison for numbers?

A

For 2004, we can assume its O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the cost of comparison for strings?

A

Worst case is O(L), where L is the length of the shortest string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the lower complexity bound for comparison based sorting algorithms?

A

Ω(N log N)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Selection Sort Algorithm

A

for i in N elements
- j = get the smallest number in [i…n]
- swap i with j

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Selection Sort Time/Space/Aux Complexity

A

Time complexity is O(N^2)
Space complexity is O(N)
Aux Space Complexity is O(1) - in place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is Selection Sort stable?

A

No!

Take the following example {3, 3, 1}

selection sort will swap the 1st and 3rd values - resulting in it being unsorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Quick Sort Big Idea?

A

If the list is less than 1: do nothing (base case)

pivot = Choose a Pivot
partition around the pivot
quick sort on the left, quicksort on the right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Complexity of QuickSort?

A

It depends :)

If the pivot is the median, then it’ll be O(n log n)
O(n) to partition, O(log n) depth
However, if the pivot happens to the smallest or largest element, then it will end up being O(n^2)
O(n) to partition, O(n) depth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the Naive QuickSort partitioning approach? (and how to make it stable)

A

Create a left, right list

- For each element which is less or equal to the pivot,
	○ Put in left
- For each element which is greater than pivot
	○ Put in right Return [left] + pivot + [right]

This won’t be stable for elements which equal the pivot
To fix this, create a centre list, and put all items equal to the pivot in this (including the pivot)
Return [left] + [pivots] + [right]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the Hoare Paritioning Algorithm (incl if it’s in place)

A

Create left_bad and right_bad pointers, equal to 0 and N respectively

While left_bad <= right_bad
- move left_bad right until we find a bad element (item > pivot)
- move right_bad left until we find a bad element (item <= pivot)
- swap these elements

this is in place (so better space complexity)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the Dutch National Flag Approach to partitioning (and what problems does it solve)?

A

Group elements into 3 groups

< pivot
= pivot
> pivot

Importantly - it creates a distinction for elements equal to the pivot

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the Dutch National Flag Algorithm?

A

3 Pointers:
1. Low - the position where the next <p should be placed
2. Mid - the current element being considered
3. High - the position where the next >p should be placed

Starting positions Low/Mid -> start of array, High -> end of array

Traverse the array from mid to high
- If the element is <p, swap it with the element at low and increment low and mid
- if the element =p, move mid forward
- if the element is >p, swap it with high and decrease high

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dutch National Flag loop invariants

A
  • Array [1, low-1] contains the <p
  • Array [low, mid-1] contains the mid
  • Array [mid, high] contains unknown items
  • Array [high to 1+n] contains the blue items
17
Q

What is our approach to choosing a pivot?

A

Solve the k-th smallest element problem.

Lets say we had this ability - we could find the median by finding n/2-th smallest element

what if we could do this in O(n)? That would ensure that QuickSort never ends up being O(n^2)

18
Q

What is the QuickSelect Algorithm?

A

Finds the k-th smallest value

If high > low:
- select a pivot
- partition the array around the pivot
- if k < mid -> quickselect on left
- if k > mid -> quickselect on right
- otherwise return array[k]

return array[k]

19
Q

What are the two options to select a pivot for QuickSelect?

A

Pick a random pivot O(n)

Median of Medians Approach
- Divide the original list of n elements into [n/5] groups of size at most 5
- Find the median of each of these groups
- Find the true median of the n/5 medians

20
Q

What is the Counting Sort algorithm (naive)

A

Determine the max possible value
Make a count array
Iterate over the input, and count the number of times each input appears
Iterate through the count array and use this to produce a sorted output

21
Q

What is the problem with the naive counting sort algorithm?

A

Not stable, and no ability to carry across data (can literally only be used for numbers)

22
Q

What is the Counting Sort Algorithm (stable)

A

(same as naive)
Determine the max possible value
Make a count array
Iterate over the input, and count the number of times each input appears

(different)
create a position array (same length as count)
- set first position to 0
- position[i] = position[i-1] + count[i-1]

iterate over the input, and for each (key, value) pair
- set output[position[key]] = (key, value)
- increment position[key]

23
Q

What is the space/time complexity of Counting Sort (and what is it good/bad for)

A

Time: O(N+U) or O(N) if U=N
Space: O(N+U) or O(N) if U=N

Therefore, this algorithm is good for when the domain is minimal
(linear time)

but very bad for when there are few values, and the domain is massive (e.g. sorting [0, 123123123123])

24
Q

What is the approach with Radix Sort?

A

Sort each position (instead of each item), and work ur way up

25
What is the algorithm with Radix sort?
Use counting sort on each digit position - make sure its stable will need to make a temp output array. make sure when you re-construct, that you bring across the whole number (not just the digit)
26
Overall Radix Sort Complexity
Time: O(k * (N + U)) - where k is the number of digits Space: O(k * (N + U)) If U = 10, then it becomes O(kN)
27
Radix Sort - customizing the base
- For N numbers, in which each number has k digits, with a base b - If the max number had a value of M, then it would have roughly logbM digits - Therefore, we will need to undertake roughly O(logbM ) count sorts - Each counting sort is O(N + b) Therefore, total complexity is O(logbM * (N + b)) This also means it may be possible to sort numbers in O(n) time - if you balance the base with the number of digits, then each interior sort is O(n + n) and overall is O(digits(constant) * n)
28
Can you apply Radix to strings?
Yes - if the strings are mapped to digits/ascii can be good - but also bad - depending on the types of strings