Ch. 5 Flashcards

(14 cards)

1
Q

Divide and Conquer

A
  1. Divide instance of problem into two or more smaller instances.
  2. Solve smaller instances recursively.
  3. Obtain solution to original (larger) instance by combining these solutions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Divide and Conquer Recurrence

A

The form T(n) = aT(n/b) + f(n)
where f(n) ∈ Θ(n^d), d >= 0

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

Common Sorting Algorithms

A

Selection Sort
Bubble Sort
Insertion Sort
Mergesort
Quicksort

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

Mergesort

A

A divide-and-conquer approach to sorting.
1. Split the array A[1..n] in two and make copies of each half in arrays B[1..⌊n/2⌋] and C[1..⌈n/2⌉].
2. Sort arrays B and C.
3. Merge sorted arrays B and C into array A.

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

Quicksort

A

A divide-and-conquer approach to sorting.
1. Select pivot point p
2. Rearrange the list so that all elements in the first s positions are ≤ p and the elements in the remaining n-s positions are ≥ p.
3. Exchange the pivot with the last element in the first subarray, and it is now in its final position.
4. Sort the two subarrays recursively

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

f(n) ∈ Θ(nᵈ)

A

f(n) runtime scales on the order of n^d.

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

Divide and Conquer Master Theorem

A

If a < bᵈ, T(n) ∈ Θ(nᵈ)
If a = bᵈ, T(n) ∈ Θ(nᵈlogn)
If a > bᵈ, T(n) ∈ Θ(n^logb(a))

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

Merge sorted arrays B and C

A

Repeat until no elements remain in one of the arrays:
- Compare the first elements in the remaining unprocessed portions of the arrays.
- Copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array.
Once all elements on one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.

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

Hoare’s Algorithm

A

Two-directional scan partitioning algorithm.
1. Repeat until i and j pointers cross:
- Scan i from left to right so long as a[i] < p
- Scan j from right to left so long as a[j] > p
2. Exchange a[i] with a[j].
3. When pointers cross, exchange p with a[j[.

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

Mergesort time complexity

A

O(nlogn)
The O(logn) comes from partitioning the array into two equal halves.
The O(n) comes from the comparisons done when merging.

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

Quicksort time complexity

A

Best and avg case: O(nlogn)
- Pivot always partitions the array equally, like mergesort.
Worst case: O(n^2)
- Pivot always ends up in first or last element and the array is partitioned as unequally as possible.

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

Master Theorem for 16T(n/4)+n

A

a = 16
b = 4
d = 1

16 > 4

Θ(n^(log₄16) = Θ(n²)

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

Master Theorem for 7T(n/3) + n²

A

a = 7
b = 3
d = 2

7 < 9

Θ(n²)

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