Divide and Conquer Algorithms
- Example
- Merge Sort
- Quick Sort
- Counting
2
Divide and Conquer
Divide the problem into a number of subproblems
• There must be base case (to stop recursion).
Conquer (solve) each subproblem recursively
Combine (merge) solutions to subproblems into a solution to the original
problem
3
Divide-and-Conquer
Most common usage.
• Break up problem of size n into two equal parts of size ½n.
• Solve two parts recursively.
• Combine two solutions into overall solution in linear time.
Consequence.
• Brute force: n2.
• Divide-and-conquer: n log n.
4
Quick Sort
Quick Sort
• It is a comparison sort where elements are sorted in place.
• This sort uses the divide-and-conquer algorithm for sorting
an array of n elements, S[1,2,…..n].
10/28/2021
Quicksort : Basic idea
Pick some number p from the array
Move all numbers less than p to the beginning of the array
Move all numbers greater than (or equal to) p to the end of the
array
Quicksort the numbers less than p
Quicksort the numbers greater than or equal to p
numbers p numbers greater than
less than p or equal to p
7
Pivot Selection
1. Use the first or last element as pivot. If the input is random, it is
acceptable. But if the input is presorted or in reverse order, pivot
provides a poor partition.
2. Choose the pivot randomly. Although a random pivot is very unlikely
provide poor partition but it is an expensive approach and does not
reduce the average running time of rest of the algorithm.
3. Choose the median as pivot. The best choice of pivot would be the
median of array. Unfortunately, it is relatively hard to calculate and
would slow down quicksort considerably.
10/28/2021
Example:
13 81 10 43 31 75 0 92
Select pivot
13 81 10 43 31 75 0 92
Partition
13 10 0 31 81 43 75 92
Quicksort
0 10 13 31 43 75 81 92
Sorted List
0 10 13 31 43 75 81 92
10/28/2021
Example
We are given array of n integers to sort:
20 80 70 80 60 50 7 30 40
10
10/28/2021
Pick Pivot Element
There are a number of ways to pick the pivot element. In this
example, we will use the last element in the array:
20 80 70 10 60 50 7 30 40
11
10/28/2021
Partitioning Array
Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.
Partitioning loops through, swapping elements
below/above pivot.
12
10/28/2021
pivot_index = 0 20 80 70 10 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
13
10/28/2021
pivot_index = 0 20 80 70 10 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
14
10/28/2021
pivot_index = 0 20 80 70 10 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
15
10/28/2021
pivot_index = 0 20 80 70 10 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
16
10/28/2021
pivot_index = 0 20 80 70 10 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
17
10/28/2021
pivot_index = 0 20 10 70 80 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
18
10/28/2021
pivot_index = 0 20 10 70 80 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
19
10/28/2021
pivot_index = 0 20 10 70 80 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
20
10/28/2021
pivot_index = 8 20 10 70 80 60 50 7 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
21
10/28/2021
pivot_index = 0 20 10 7 80 60 50 70 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
22
10/28/2021
pivot_index = 0 20 10 7 80 60 50 70 30 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
23
10/28/2021
pivot_index = 0 20 10 7 30 60 50 70 80 40
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
24
10/28/2021
pivot_index = 0 20 10 7 30 40 50 70 80 60
[0] [1] [2] [3] [4] [5] [6] [7] [8]
25
10/28/2021
Partition Result
20 10 7 30 40 50 70 80 60
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
26
10/28/2021
Recursion: Quicksort Sub-arrays
20 10 7 30 40 50 70 80 60
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
27
10/28/2021
Quicksort
To sort a[left...right]:
1. if left < right:
1.1. Partition a[left...right] such that:
all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate
28
Partitioning (Quicksort )
A key step in the Quicksort algorithm is partitioning the
array
We choose some (any) number p in the array to use as a pivot
We partition the array into three parts:
numbers p numbers greater than
less than p or equal to p
29
Partitioning
Choose an array value (say, the first) to use as the
pivot
Starting from the left end, find the first element
that is greater than or equal to the pivot
Searching backward from the right end, find the
first element that is less than the pivot
Interchange (swap) these two elements
Repeat, searching from where we left off, until
done
30
Partitioning
To partition a[left...right]:
1. Set pivot = a[left], l = left + 1, r = right;
2. while l < r, do
2.1. while l < right & a[l] < pivot , set l = l + 1
2.2. while r > left & a[r] >= pivot , set r = r - 1
2.3. if l < r, swap a[l] and a[r]
3. Set a[left] = a[r], a[r] = pivot
4. Terminate
31
Example of partitioning
choose pivot: 436924312189356
search: 436924312189356
swap: 433924312189656
search: 433924312189656
swap: 433124312989656
search: 433124312989656
swap: 433122314989656
search: 433122314989656 (left > right)
swap with pivot: 133122344989656
32
Algorithm
quickSort(arr[], low, high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is now
at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi - 1); // Before pi
quickSort(arr, pi + 1, high); // After pi
}
}
Merge Sort p q r
1 2 3 4 5 6 7 8
Alg.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6
if p < r Check for base case
then q ← (p + r)/2 Divide
MERGE-SORT(A, p, q) Conquer
MERGE-SORT(A, q + 1, r) Conquer
MERGE(A, p, q, r) Combine
• Initial call: MERGE-SORT(A, 1, n)
34
Merge Function
Example
1 2 3 4 5 6 7 8
Divide 5 2 4 7 1 3 2 6 q=4
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
36
Example – n Power of 2
1 2 3 4 5 6 7 8
Conquer 1 2 2 3 4 5 6 7
and
Merge 1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
37
Example
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6 q=6
Divide
1 2 3 4 5 6 7 8 9 10 11
q=3 4 7 2 6 1 4 7 3 5 2 6 q=9
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 6 1 4 7 3 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
38
Example –
1 2 3 4 5 6 7 8 9 10 11
Conquer 1 2 2 3 4 4 5 6 6 7 7
and
Merge
1 2 3 4 5 6 7 8 9 10 11
1 2 4 4 6 7 2 3 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 4 7 1 4 6 3 5 7 2 6
1 2 3 4 5 6 7 8 9 10 11
4 7 2 1 6 4 3 7 5 2 6
1 2 4 5 7 8
4 7 6 1 7 3
39
Example