Divide and Conquer
Divide and Conquer
DAA
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
smallest smallest
A G L O R H I M S T
A auxiliary array
08/08/2024 19
Example
• 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
08/08/2024 20
Example
• 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
08/08/2024 21
Example
• 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
08/08/2024 22
Example
• 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
08/08/2024 23
Example
• 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
08/08/2024 24
Example
• 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
08/08/2024 25
Example
• 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
08/08/2024 26
Example
• 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
08/08/2024 27
Example
• 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
08/08/2024 28
Example
• 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
08/08/2024 29
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e., ) subarray
— the pivot is now in its final position
• Sort the two subarrays recursively
Aug 8, 2024 DAA-Dr.S.Durai 33
Quick sort Algorithm
• Improvements:
– better pivot selection: median of three partitioning
– switch to insertion sort on small subfiles
– elimination of recursion
These combine to 20-25% improvement
Aug 8, 2024 DAA-Dr.S.Durai 38
HEAP SORT
25
22 17
19 22 14 15
18 14 21 3 9 11
11
22 17
19 22 14 15
18 14 21 3 9 11
11
22 17
19 22 14 15
18 14 21 3 9
22
22 17
19 11 14 15
18 14 21 3 9
22
22 17
19 21 14 15
18 14 11 3 9
– Cw (1) = 1
solution: Cw(n) = log2(n+1)
Pmax
P2
P1