Ch. 5 Flashcards
(14 cards)
Divide and Conquer
- Divide instance of problem into two or more smaller instances.
- Solve smaller instances recursively.
- Obtain solution to original (larger) instance by combining these solutions.
Divide and Conquer Recurrence
The form T(n) = aT(n/b) + f(n)
where f(n) ∈ Θ(n^d), d >= 0
Common Sorting Algorithms
Selection Sort
Bubble Sort
Insertion Sort
Mergesort
Quicksort
Mergesort
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.
Quicksort
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
f(n) ∈ Θ(nᵈ)
f(n) runtime scales on the order of n^d.
Divide and Conquer Master Theorem
If a < bᵈ, T(n) ∈ Θ(nᵈ)
If a = bᵈ, T(n) ∈ Θ(nᵈlogn)
If a > bᵈ, T(n) ∈ Θ(n^logb(a))
Merge sorted arrays B and C
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.
Hoare’s Algorithm
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[.
Mergesort time complexity
O(nlogn)
The O(logn) comes from partitioning the array into two equal halves.
The O(n) comes from the comparisons done when merging.
Quicksort time complexity
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.
Master Theorem for 16T(n/4)+n
a = 16
b = 4
d = 1
16 > 4
Θ(n^(log₄16) = Θ(n²)
Master Theorem for 7T(n/3) + n²
a = 7
b = 3
d = 2
7 < 9
Θ(n²)