Algorithm Design & Analysis
SE 208
Unit 2-1
Searching and Sorting (Divide and Conquer)
Divyashikha Sethia
[email protected]
DTU
Divide and Conquer Algorithms
• Basic Idea
• Binary Search
• Merge Sort
• Quick Sort
• Stassen Multiplication
• Heap Sort
• Analysis Divide and Conquer run time recurrence
relation
• Conclusion
2
Basic Idea
◼Divide instance of problem into two or more smaller instances
◼Conquer: Solve smaller instances recursively
◼Combine: Obtain solution to original (larger) instance by combining
these solutions
3
Basic Idea
a problem of size n
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
4
General Divide-and-Conquer Recurrence
T(n) = aT(n/b) + f (n) where f(n) (nd), • Case 1:
d0
nlogba: compared with nd – If f(n) is dominated by
nlogba:
Master Theorem: If a < bd, T(n) (nd) • T(n) = (nlogbn)
case 3
If a = bd, T(n) (nd log n) case 2
If a > bd, T(n) (nlog b a ) case 1 • Case 3:
◼Examples: – If f(n) dominates nlogba:
T(n) = T(n/2) + n T(n) (n ) • T(n) = (f(n))
Here a = 1, b = 2, d = 1, a < bd
T(n) = 2T(n/2) + 1 T(n) (n log 2 2 ) = (n ) • Case 2:
Here a = 2, b = 2, d = 0, a > bd – If f(n) = (nlogba):
• T(n) = (nlogba logn)
5
Examples
◼T(n) = T(n/2) + 1 T(n) (log(n) )
Here a = 1, b = 2, d = 0, a = bd
◼T(n) = 4T(n/2) + n T(n) (n log 2 4 ) = (n 2 )
Here a = 4, b = 2, d = 1, a > bd
◼ T(n) = 4T(n/2) + n2 T(n) (n2 log n)
Here a = 4, b = 2, d = 2, a = bd
◼T(n) = 4T(n/2) + n3 T(n) (n3)
Here a = 4, b = 2, d = 3, a < bd
6
Binary Search
◼Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
◼If K = A[m], stop (successful search);
◼otherwise, continue searching by the same method
in A[0..m-1] if K < A[m],
and in A[m+1..n-1] if K > A[m]
7
Analysis of Binary Search
• Time efficiency T(N) = T(N/2) + 1
• Worst case: O(log(n))
• Best case: O(1)
• Optimal for searching a sorted array
• Limitations: must be a sorted array
(not linked list)
8
9
10
Mergesort
◼Split array A[0..n-1] in two about equal halves and
make copies of each half in arrays B and C
◼Sort arrays B and C recursively
◼Merge sorted arrays B and C into array A
11
Mergesort
12
Mergesort
13
Mergesort
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 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
14
Mergesort Code
mergesort( int a[], int left, int right)
{
if (right > left)
{
middle = left + (right - left)/2;
mergesort(a, left, middle);
mergesort(a, middle+1, right);
merge(a, left, middle, right);
}
}
15
16
17
Analysis of Mergesort
• T(N) = 2T(N/2) + N
• All cases have same efficiency: Θ(n log n)
• Space requirement: Θ(n) (not in-place)
• Can be implemented without recursion (bottom-up)
18
Features of Mergesort
• All cases: Θ(n log n)
• Requires additional memory Θ(n)
• Recursive
• Not stable (does not preserve previous sorting)
• Never used for sorting in main memory, used for external sorting
19
• Referenecs
• Cormen Chapter 4
20