Lecture10 DC
Lecture10 DC
-
Example
- Binary Search
- Merge Sort
- Quick Sort
- Counting
- Closest Pair of Points
2
Divide and Conquer
3
Divide-and-Conquer
Consequence.
Brute force: n2.
Divide-and-conquer: n log n.
4
Quicksort
10/15/20 5
Quick Sort
10/15/20
Quicksort Algorithm
10/15/20
Pivot Selection
10/15/20
Quicksort Algorithm
10/15/20
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
10/15/20
Example
11
10/15/20
Pick Pivot Element
12
10/15/20
Partitioning Array
13
10/15/20
pivot_index = 0 20 80 70 10 60 50 7 30 40
i j
14
10/15/20
pivot_index = 0 20 80 70 10 60 50 7 30 40
i j
15
10/15/20
pivot_index = 0 20 80 70 10 60 50 7 30 40
i j
16
10/15/20
pivot_index = 0 20 80 70 10 60 50 7 30 40
i j
17
10/15/20
pivot_index = 0 20 80 70 10 60 50 7 30 40
i j
18
10/15/20
pivot_index = 0 20 10 70 80 60 50 7 30 40
i j
19
10/15/20
pivot_index = 0 20 10 70 80 60 50 7 30 40
i j
20
10/15/20
pivot_index = 0 20 10 70 80 60 50 7 30 40
i j
21
10/15/20
pivot_index = 8 20 10 70 80 60 50 7 30 40
i j
22
10/15/20
pivot_index = 0 20 10 7 80 60 50 70 30 40
i j
23
10/15/20
pivot_index = 0 20 10 7 80 60 50 70 30 40
i j
24
10/15/20
pivot_index = 0 20 10 7 30 60 50 70 80 40
i j
25
10/15/20
pivot_index = 0 20 10 7 30 40 50 70 80 60
26
10/15/20
Partition Result
20 10 7 30 40 50 70 80 60
27
10/15/20
Recursion: Quicksort Sub-arrays
20 10 7 30 40 50 70 80 60
28
10/15/20
Complexity of Quick Sort
1. Worst Case:
The worst case occurs if the input is presorted or in reverse order
and if the 1st or the last element is chosen as pivot. In this case
pivot provides a poor partition because all elements go into one
partition and the other partition is empty. The worst case running
time is 0(N2).
2. Average Case:
The average running time of Quick sort is obtained by averaging
the runtime of all possible subproblems’ sizes. That means it
occurs from the fact that on average each reduction step of the
algorithm produces two subsets. So, the average running time is
0(NlogN).
3. Best Case:
The best case occurs when the partition always splits into two
subsets with equal number of elements. So, the running time is 29
0(NlogN). 10/15/20
Mergesort
10/15/20 30
Merge Sort
• Merge sort is a comparison sorting technique.
• This technique follows the divide-and-conquer approach.
• It maintains the following 3 steps:
1. Divide: Divide N-element sequence to be sorted into two
subsequences of
about N/2 elements each and sort the two subsequences
recursively .
• Merge sort uses the “merge” step to combine two sorted sublists to create
one single
sorted list.
• Suppose A is a sorted list with R elements and B is another sorted list with
S elements.
After merging there is only a single sorted list C with N=R+S elements.
31
10/15/20
Mergesort Algorithm
32
10/15/20
Example:
5 2 4 1 7 3 2 6
5 2 4 1 7 3 2 6
5 2 4 1 7 3 2 6
5 2 4 1 7 3 2 6
2 5 1 4 3 7 2 6
1 2 4 5 2 3 6 7
1 2 2 3 4 5 6 7
33
10/15/20
Example:
Suppose Array A = 38, 27, 43, 3, 9, 82 and 10. Sort the array using Mergesort.
10/15/20
34
Merging
A G L O R H I M S T
A G H I
35
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A auxiliary array
36
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
37
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
38
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I auxiliary array
39
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L auxiliary array
40
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M auxiliary array
41
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O auxiliary array
42
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
43
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
44
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S T auxiliary array
45
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
A G L O R H I M S T
A G H I L M O R S T auxiliary array
46
A Useful Recurrence Relation
Mergesort recurrence.
47
Proof by Recursion Tree
T(n) n
...
T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) n/2 (2)
n log2n
48
Proof by Telescoping
assumes n is a power of 2
49
Proof by Induction
assumes n is a power of 2
50