KEMBAR78
module2_dIVIDEncONQUER_2022.pdf
MODULE 2
DIVIDE AND CONQUER
The General Method
1. DIVIDE: Reduce problem instance to smaller instance of the same
problem
2. CONQUER: Solve smaller instance recursively, independently
3. COMBINE: Extend solution of smaller instances to obtain solution
to original instance
Shiwani Gupta 2
Applications of D & C Appoach
• Binary Search
• Max Min Problem
• Merge Sort
• Quick Sort
• Strassen’s Matrix Multiplication
• Problem of multiplying long integers
• Constructing tennis tournament
Shiwani Gupta 3
Sequential Search
Algorithm SequentialSearch(A[0…n-1], K)
//Problem Description: Searches for a given value in a given array by
// sequential search
//Input: An Array A[0…n-1] and a search key K
//Output: Returns the index of the first element of A that matches K
// or -1 if there are no matching elements
i  0
while i < n and A[i]  K do
i  i+1
if i < n return i
else return -1
1. Worst case: O(n)
2. Best case: O(1)
3. Average case: O(n/2)
Thus, we say sequential search is O(n)
Shiwani Gupta 4
Binary Search
Search requires the following steps:
1. Inspect the middle item of an array of size N.
2. Inspect the middle of an array of size N/2.
3. Inspect the middle item of an array of size N/power(2,2) and so on
until N/power(2,k) = 1.
– This implies k = log2N
– k is the number of partitions.
• Requires that the array be sorted.
• Rather than start at either end, binary search splits the array in
half and works only with the half that may contain the value.
• This action of dividing continues until the desired value is found
or the remaining values are either smaller or larger than the
search value.
Shiwani Gupta 5
Binary Search: pseudo-code (ITERATIVE)
binary_search(a, x)
left ← 0
right ← N-1
while(left <= right)
mid= (left+right)/2
if (a[mid] > x)
right ← mid – 1
else if (x > a[mid])
low ← mid + 1
else
return mid //found
return -1 //not found
Shiwani Gupta 6
Binary Search: pseudo-code (RECURSIVE)
binary_search(a, x, left, right)
if (right < left)
return -1 // not found
mid= floor((left+right)/2) //floor(.)=(int)(.)
if (a[mid] == x)
return mid
else if (x < a[mid])
binary_search(a, x, left, mid-1)
else
binary_search(a, x, mid+1, right)
Binary Search: Time Complexity (for successful search)
Number of comparisons:
T(n) = T(n/2) + 1, if n>1;
= 1, if n=1
which solves to T(n) = Ѳ(logn) with Case 2 of Master Method. 7
Search algorithm efficiency
• Sequential search: (n)
• Binary search: (log2n)
– no more than 10 iterations, to find an element in a sorted
list of 1000 elements
– no more than 20 iterations, for a sorted list of one
million!
Shiwani Gupta 8
Finding Max Min
StraightMaxMin(a, n, max, min)
max = min = a[1]
for i = 2 to n do
if (a[i]>max) then max = a[i] //n-1 comparisons
if (a[i]<min) then min = a[i] //n-1 comparisons
Total = 2*(n-1) comparisons
1. Worst case: O(n)
2. Best case: O(n)
3. Average case: O(n)
Thus, we say Straight Max Min is O(n).
Shiwani Gupta 9
MaxMin(i,j,max,min) //1<=i<=j<=n
if ( i == j) then max = min = a[i] //single element
else if (i == j -1) then //two elements
if (a[i] < a[j]) then
max = a[j]
min = a[i]
else
max = a[i]
min = a[j]
else //divide P into subproblems
mid = (i+j)/2 //find split
MaxMin(i, mid, max, min) //solve subproblems
MaxMin(mid+1, j, max1, min1) //solve subproblems
if (max<max1) then max = max1 //combine solutions
if (min>min1) then min = min1 //combine solutions
The recurrence for this algorithm is
T(n) = 2T(n/2) + 2 n>2 when
max and min are considered as different procedures
T(n) = 1 n=2
T(n) = 0 n=1
This solves to T(n) = 3n/2-2.
Finding Max Min
10
Solving recurrence by substitution
Shiwani Gupta 11
Merge Sort
Sorting Problem: Sort a sequence of n elements into non-
decreasing order.
• Divide: Divide the n-element sequence to be sorted into
two subsequences of n/2 elements each
• Conquer: Sort the two subsequences recursively using
merge sort.
• Combine: Merge the two sorted subsequences to produce
the sorted answer.
Shiwani Gupta 12
Merge Sort – Example
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
26
18 6
32 15
43 1
9
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32
6 15 43 1 9
6 18 26 32 1 9 15 43
1 6 9 15 18 26 32 43
18 26
18 26
18 26
32
32
6
6
32 6
18 26 32 6
43
43
15
15
43 15
9
9
1
1
9 1
43 15 9 1
18 26 32 6 43 15 9 1
18 26 6
32
6
26 32
18
15
43 1
9
1 9
15 43
1
6 9 15
18 26 32 43
Original Sequence Sorted Sequence
Shiwani Gupta 14
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
MergeSort (A, p, r) // p as beginning pointer of array and r as end pointer
1 if p < r
2 then q  (p+r)/2 //split the list at mid
3 MergeSort (A, p, q) //first sublist
4 MergeSort (A, q+1, r) //second sublist
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]
Initial Call: MergeSort(A, 1, n)
Shiwani Gupta 15
Procedure Merge
Merge(A, p, q, r)
1 n1  q – p + 1
2 n2  r – q
3 for i  1 to n1
4 do L[i]  A[p + i – 1]
5 for j  1 to n2
6 do R[j]  A[q + j]
7 L[n1+1]  
8 R[n2+1]  
9 i  1
10 j  1
11 for k p to r
12 do if L[i]  R[j]
13 then A[k]  L[i]
14 i  i + 1
15 else A[k]  R[j]
16 j  j + 1
Sentinels, to avoid having to
check if either subarray is
fully copied at each step.
Input: Array containing sorted
subarrays A[p..q] and
A[q+1..r].
Output: Merged sorted
subarray in A[p..r].
Shiwani Gupta 16
j
Merge – Example
6 8 26 32 1 9 42 43
… …
A
k
6 8 26 32 1 9 42 43
k k k k k k k
i i i i
 
i j j j j
6 8 26 32 1 9 42 43
1 6 8 9 26 32 42 43
k
L R
Shiwani Gupta 17
Analysis of Merge Sort – Master’s Method
• Running time T(n) of Merge Sort:
• Divide: computing the middle takes (1)
• Conquer: solving 2 subproblems takes 2T(n/2)
• Combine: merging n elements takes (n)
• Total:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
 T(n) = (n lg n) case 2
Shiwani Gupta 18
• Running time of Merge Sort:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
• Rewrite the recurrence as
T(n) = c if n = 1
T(n) = 2T(n/2) + cn if n > 1
c > 0: Running time for the base case and time per array
element for the divide and combine steps.
Analysis of Merge Sort – Recursion Tree
Method
Shiwani Gupta 19
Recursion Tree for Merge Sort
For the original problem,
we have a cost of cn,
plus two subproblems
each of size (n/2) and
running time T(n/2).
cn
T(n/2) T(n/2)
Each of the size n/2 problems
has a cost of cn/2 plus two
subproblems, each costing
T(n/4).
cn
cn/2 cn/2
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of divide and
merge.
Cost of sorting
subproblems.
Shiwani Gupta 20
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c c
c c
lg n + 1
cn
cn
cn
cn
Total : cnlgn+cn
Shiwani Gupta 21
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn
cn/2 cn/2
cn/4 cn/4 cn/4 cn/4
c c c c
c c
•Each level has total cost cn.
•Each time we go down one level, the
number of subproblems doubles, but the
cost per subproblem halves  cost per
level remains the same.
•There are lg n + 1 levels, height is lg n.
(Assuming n is a power of 2.)
• Can be proved by induction.
•Total cost = sum of costs at each level
= (lg n + 1)cn = cnlgn + cn = (n lgn).
Shiwani Gupta 22
Quicksort
• Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1] and
A[s+1..r] such that each element of the first array is ≤ A[s]
and each element of the second array is ≥ A[s]. (computing
the index of s is part of partition.)
– Implication: A[s] will be in its final position in the sorted
array.
• Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r] by
recursive calls to Quicksort
• Combine: No work is needed, because A[s] is already in its
correct place after the partition is done, and the two
subarrays have been sorted.
Shiwani Gupta 23
The Quicksort Algorithm
ALGORITHM Quicksort(A[l..r])
//Problem Description: Sorts a subarray by quicksort
//Input: A subarray A[l..r] of A[0..n-1],defined by its left and right
indices l and r
//Output: The subarray A[l..r] sorted in nondecreasing order
if l < r
s  Partition (A[l..r]) // s is a split position
Quicksort(A[l..s-1])
Quicksort(A[s+1..r]
Shiwani Gupta 24
Partitioning Algorithm
Algorithm Partition(A[l, r])
p  A[l ]
i  l; j  r+1
repeat
repeat i←i+1 until A[i]>p
repeat j←j-1 until A[i]<=p
swap(A[i],A[j])
until i>j
swap(p,A[j])
Shiwani Gupta 25
Example
We are given array of n integers to sort:
40 20 10 80 60 50 7 30 100
Shiwani Gupta 26
Pick Pivot Element
There are a number of ways to pick the pivot element. In this example,
we will use the first element in the array:
40 20 10 80 60 50 7 30 100
Shiwani Gupta 27
40 20 10 80 60 50 7 30 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 28
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
p = 0
Shiwani Gupta 29
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
p = 0
Shiwani Gupta 30
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
p = 0
Shiwani Gupta 31
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
p = 0
Shiwani Gupta 32
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
p = 0
Shiwani Gupta 33
40 20 10 80 60 50 7 30 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
p = 0
Shiwani Gupta 34
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
p = 0
Shiwani Gupta 35
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
p = 0
Shiwani Gupta 36
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
p = 0
Shiwani Gupta 37
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
p = 0
Shiwani Gupta 38
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
p = 0
Shiwani Gupta 39
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
p = 0
Shiwani Gupta 40
40 20 10 30 60 50 7 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
p = 0
Shiwani Gupta 41
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 42
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 43
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 44
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 45
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 46
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 47
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
p = 0
Shiwani Gupta 48
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 49
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
40 20 10 30 7 50 60 80 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 50
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
40 20 10 30 7 50 60 80 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 51
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
7 20 10 30 40 50 60 80 100
p = 4
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 52
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[p] > data[p]
Partition Result
Best Case
Shiwani Gupta 53
Quicksort: Worst Case
• Assume first element is chosen as p.
• Assume we get array that is already in order:
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 54
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 55
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 56
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 57
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 58
1. while data[i] <= data[p]
++i
2. while data[j] > data[p]
--j
3. if i < j
swap data[i] and data[j]
4. while j > i, go to 1.
5. swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 59
1. While data[i] <= data[p]
++i
2. While data[j] > data[p]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
5. Swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Shiwani Gupta 60
1. While data[i] <= data[p]
++i
2. While data[j] > data[p]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
5. Swap data[j] and data[p]
2 4 10 12 13 50 57 63 100
p = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
> data[p]
<= data[p]
Shiwani Gupta 61
Efficiency of Quicksort
Based on whether the partitioning is balanced.
• Best case: split in the middle — Θ( n log n)
– T(n) = 2T(n/2) + Θ(n) , n>1 //2 subproblems of size n/2 each
– T(1)=0
• Worst case: sorted array — Θ( n2)
– T(n) = T(n-1) + T(0) + Θ(n) //2 subproblems of size 0 and n-1
– T(0)= 0
• Average case: random arrays — Θ( n log n) //of size 9:1 or 99:1
62
Solving Worst case recurrence
Solving Best case recurrence
Solving Average case recurrence
log4/3 n levels = log2 n / log2 4/3 = log n
T(n) = nlogn
Improve Efficiency of Quick Sort
• Use mid element as pivot
• Take the median as pivot
• Take mean of first, last and middle element as pivot
• Use random element as pivot
– Running time is independent of input ordering
– Worst case determined only by output of random-number generator
Shiwani Gupta 66
What is divide and conquer method?
Show that algorithm for finding
Minimum and Maximum using Divide
and Conquer does not use more than 3n/2
comparisons
Implement recursive Binary Search algorithm. Write
complete code along with recursive function call
Implement the Binary Search, prove that the
complexity of binary search is O(log2n)
Explain all sorting techniques based on divide and conquer strategy
Sort the given elements using Merge Sort
technique. 90, 20 80, 89, 70, 65, 85, 74.
Show passes both for divide and combine
Solve Merge sort recurrence using
recursion tree method
Give the analysis of Merge Sort using
divide and conquer?
Write Merge sort algorithm and analyze it
Sort following data using Merge Sort 9,
8, 7, 6, 5, 4, 3, 2, 1, 0
Give the analysis of Merge Sort using
Divide and Conquer?
Sort the given elements using Merge Sort
technique. 90, 20 80, 89, 70, 65, 85, 74
Show how Quick sort can be made to run in
O(nlogn) time in worst case
Analyze worst and best case complexity for Quick
sort
Explain analysis of Quick Sort algorithm using
recursion
Sort following data using Quick Sort 9, 8, 7, 6, 5, 4,
3, 2, 1, 0
Sort the given elements using Quick Sort technique.
90, 20 80, 89, 70,65, 85, 74
Prove that worst case complexity of Quick Sort is
O(n2)
Analyze worst and best case complexity for Quick
Sort
Task

module2_dIVIDEncONQUER_2022.pdf

  • 1.
  • 2.
    The General Method 1.DIVIDE: Reduce problem instance to smaller instance of the same problem 2. CONQUER: Solve smaller instance recursively, independently 3. COMBINE: Extend solution of smaller instances to obtain solution to original instance Shiwani Gupta 2
  • 3.
    Applications of D& C Appoach • Binary Search • Max Min Problem • Merge Sort • Quick Sort • Strassen’s Matrix Multiplication • Problem of multiplying long integers • Constructing tennis tournament Shiwani Gupta 3
  • 4.
    Sequential Search Algorithm SequentialSearch(A[0…n-1],K) //Problem Description: Searches for a given value in a given array by // sequential search //Input: An Array A[0…n-1] and a search key K //Output: Returns the index of the first element of A that matches K // or -1 if there are no matching elements i  0 while i < n and A[i]  K do i  i+1 if i < n return i else return -1 1. Worst case: O(n) 2. Best case: O(1) 3. Average case: O(n/2) Thus, we say sequential search is O(n) Shiwani Gupta 4
  • 5.
    Binary Search Search requiresthe following steps: 1. Inspect the middle item of an array of size N. 2. Inspect the middle of an array of size N/2. 3. Inspect the middle item of an array of size N/power(2,2) and so on until N/power(2,k) = 1. – This implies k = log2N – k is the number of partitions. • Requires that the array be sorted. • Rather than start at either end, binary search splits the array in half and works only with the half that may contain the value. • This action of dividing continues until the desired value is found or the remaining values are either smaller or larger than the search value. Shiwani Gupta 5
  • 6.
    Binary Search: pseudo-code(ITERATIVE) binary_search(a, x) left ← 0 right ← N-1 while(left <= right) mid= (left+right)/2 if (a[mid] > x) right ← mid – 1 else if (x > a[mid]) low ← mid + 1 else return mid //found return -1 //not found Shiwani Gupta 6
  • 7.
    Binary Search: pseudo-code(RECURSIVE) binary_search(a, x, left, right) if (right < left) return -1 // not found mid= floor((left+right)/2) //floor(.)=(int)(.) if (a[mid] == x) return mid else if (x < a[mid]) binary_search(a, x, left, mid-1) else binary_search(a, x, mid+1, right) Binary Search: Time Complexity (for successful search) Number of comparisons: T(n) = T(n/2) + 1, if n>1; = 1, if n=1 which solves to T(n) = Ѳ(logn) with Case 2 of Master Method. 7
  • 8.
    Search algorithm efficiency •Sequential search: (n) • Binary search: (log2n) – no more than 10 iterations, to find an element in a sorted list of 1000 elements – no more than 20 iterations, for a sorted list of one million! Shiwani Gupta 8
  • 9.
    Finding Max Min StraightMaxMin(a,n, max, min) max = min = a[1] for i = 2 to n do if (a[i]>max) then max = a[i] //n-1 comparisons if (a[i]<min) then min = a[i] //n-1 comparisons Total = 2*(n-1) comparisons 1. Worst case: O(n) 2. Best case: O(n) 3. Average case: O(n) Thus, we say Straight Max Min is O(n). Shiwani Gupta 9
  • 10.
    MaxMin(i,j,max,min) //1<=i<=j<=n if (i == j) then max = min = a[i] //single element else if (i == j -1) then //two elements if (a[i] < a[j]) then max = a[j] min = a[i] else max = a[i] min = a[j] else //divide P into subproblems mid = (i+j)/2 //find split MaxMin(i, mid, max, min) //solve subproblems MaxMin(mid+1, j, max1, min1) //solve subproblems if (max<max1) then max = max1 //combine solutions if (min>min1) then min = min1 //combine solutions The recurrence for this algorithm is T(n) = 2T(n/2) + 2 n>2 when max and min are considered as different procedures T(n) = 1 n=2 T(n) = 0 n=1 This solves to T(n) = 3n/2-2. Finding Max Min 10
  • 11.
    Solving recurrence bysubstitution Shiwani Gupta 11
  • 12.
    Merge Sort Sorting Problem:Sort a sequence of n elements into non- decreasing order. • Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each • Conquer: Sort the two subsequences recursively using merge sort. • Combine: Merge the two sorted subsequences to produce the sorted answer. Shiwani Gupta 12
  • 13.
    Merge Sort –Example 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 26 18 6 32 15 43 1 9 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 15 43 1 9 6 18 26 32 1 9 15 43 1 6 9 15 18 26 32 43 18 26 18 26 18 26 32 32 6 6 32 6 18 26 32 6 43 43 15 15 43 15 9 9 1 1 9 1 43 15 9 1 18 26 32 6 43 15 9 1 18 26 6 32 6 26 32 18 15 43 1 9 1 9 15 43 1 6 9 15 18 26 32 43 Original Sequence Sorted Sequence Shiwani Gupta 14
  • 14.
    Merge-Sort (A, p,r) INPUT: a sequence of n numbers stored in array A OUTPUT: an ordered sequence of n numbers MergeSort (A, p, r) // p as beginning pointer of array and r as end pointer 1 if p < r 2 then q  (p+r)/2 //split the list at mid 3 MergeSort (A, p, q) //first sublist 4 MergeSort (A, q+1, r) //second sublist 5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r] Initial Call: MergeSort(A, 1, n) Shiwani Gupta 15
  • 15.
    Procedure Merge Merge(A, p,q, r) 1 n1  q – p + 1 2 n2  r – q 3 for i  1 to n1 4 do L[i]  A[p + i – 1] 5 for j  1 to n2 6 do R[j]  A[q + j] 7 L[n1+1]   8 R[n2+1]   9 i  1 10 j  1 11 for k p to r 12 do if L[i]  R[j] 13 then A[k]  L[i] 14 i  i + 1 15 else A[k]  R[j] 16 j  j + 1 Sentinels, to avoid having to check if either subarray is fully copied at each step. Input: Array containing sorted subarrays A[p..q] and A[q+1..r]. Output: Merged sorted subarray in A[p..r]. Shiwani Gupta 16
  • 16.
    j Merge – Example 68 26 32 1 9 42 43 … … A k 6 8 26 32 1 9 42 43 k k k k k k k i i i i   i j j j j 6 8 26 32 1 9 42 43 1 6 8 9 26 32 42 43 k L R Shiwani Gupta 17
  • 17.
    Analysis of MergeSort – Master’s Method • Running time T(n) of Merge Sort: • Divide: computing the middle takes (1) • Conquer: solving 2 subproblems takes 2T(n/2) • Combine: merging n elements takes (n) • Total: T(n) = (1) if n = 1 T(n) = 2T(n/2) + (n) if n > 1  T(n) = (n lg n) case 2 Shiwani Gupta 18
  • 18.
    • Running timeof Merge Sort: T(n) = (1) if n = 1 T(n) = 2T(n/2) + (n) if n > 1 • Rewrite the recurrence as T(n) = c if n = 1 T(n) = 2T(n/2) + cn if n > 1 c > 0: Running time for the base case and time per array element for the divide and combine steps. Analysis of Merge Sort – Recursion Tree Method Shiwani Gupta 19
  • 19.
    Recursion Tree forMerge Sort For the original problem, we have a cost of cn, plus two subproblems each of size (n/2) and running time T(n/2). cn T(n/2) T(n/2) Each of the size n/2 problems has a cost of cn/2 plus two subproblems, each costing T(n/4). cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4) Cost of divide and merge. Cost of sorting subproblems. Shiwani Gupta 20
  • 20.
    Recursion Tree forMerge Sort Continue expanding until the problem size reduces to 1. cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 c c c c c c lg n + 1 cn cn cn cn Total : cnlgn+cn Shiwani Gupta 21
  • 21.
    Recursion Tree forMerge Sort Continue expanding until the problem size reduces to 1. cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 c c c c c c •Each level has total cost cn. •Each time we go down one level, the number of subproblems doubles, but the cost per subproblem halves  cost per level remains the same. •There are lg n + 1 levels, height is lg n. (Assuming n is a power of 2.) • Can be proved by induction. •Total cost = sum of costs at each level = (lg n + 1)cn = cnlgn + cn = (n lgn). Shiwani Gupta 22
  • 22.
    Quicksort • Divide: Partitionarray A[l..r] into 2 subarrays, A[l..s-1] and A[s+1..r] such that each element of the first array is ≤ A[s] and each element of the second array is ≥ A[s]. (computing the index of s is part of partition.) – Implication: A[s] will be in its final position in the sorted array. • Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r] by recursive calls to Quicksort • Combine: No work is needed, because A[s] is already in its correct place after the partition is done, and the two subarrays have been sorted. Shiwani Gupta 23
  • 23.
    The Quicksort Algorithm ALGORITHMQuicksort(A[l..r]) //Problem Description: Sorts a subarray by quicksort //Input: A subarray A[l..r] of A[0..n-1],defined by its left and right indices l and r //Output: The subarray A[l..r] sorted in nondecreasing order if l < r s  Partition (A[l..r]) // s is a split position Quicksort(A[l..s-1]) Quicksort(A[s+1..r] Shiwani Gupta 24
  • 24.
    Partitioning Algorithm Algorithm Partition(A[l,r]) p  A[l ] i  l; j  r+1 repeat repeat i←i+1 until A[i]>p repeat j←j-1 until A[i]<=p swap(A[i],A[j]) until i>j swap(p,A[j]) Shiwani Gupta 25
  • 25.
    Example We are givenarray of n integers to sort: 40 20 10 80 60 50 7 30 100 Shiwani Gupta 26
  • 26.
    Pick Pivot Element Thereare a number of ways to pick the pivot element. In this example, we will use the first element in the array: 40 20 10 80 60 50 7 30 100 Shiwani Gupta 27
  • 27.
    40 20 1080 60 50 7 30 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 28
  • 28.
    40 20 1080 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i p = 0 Shiwani Gupta 29
  • 29.
    40 20 1080 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i p = 0 Shiwani Gupta 30
  • 30.
    40 20 1080 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i p = 0 Shiwani Gupta 31
  • 31.
    40 20 1080 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j p = 0 Shiwani Gupta 32
  • 32.
    40 20 1080 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j p = 0 Shiwani Gupta 33
  • 33.
    40 20 1080 60 50 7 30 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] p = 0 Shiwani Gupta 34
  • 34.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] p = 0 Shiwani Gupta 35
  • 35.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. p = 0 Shiwani Gupta 36
  • 36.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. p = 0 Shiwani Gupta 37
  • 37.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. p = 0 Shiwani Gupta 38
  • 38.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. p = 0 Shiwani Gupta 39
  • 39.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. p = 0 Shiwani Gupta 40
  • 40.
    40 20 1030 60 50 7 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j 1. while data[i] <= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. p = 0 Shiwani Gupta 41
  • 41.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 42
  • 42.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 43
  • 43.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 44
  • 44.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 45
  • 45.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 46
  • 46.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 47
  • 47.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j p = 0 Shiwani Gupta 48
  • 48.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 49
  • 49.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 40 20 10 30 7 50 60 80 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 50
  • 50.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 40 20 10 30 7 50 60 80 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 51
  • 51.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 7 20 10 30 40 50 60 80 100 p = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 52
  • 52.
    7 20 1030 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[p] > data[p] Partition Result Best Case Shiwani Gupta 53
  • 53.
    Quicksort: Worst Case •Assume first element is chosen as p. • Assume we get array that is already in order: 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 54
  • 54.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 55
  • 55.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 56
  • 56.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 57
  • 57.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 58
  • 58.
    1. while data[i]<= data[p] ++i 2. while data[j] > data[p] --j 3. if i < j swap data[i] and data[j] 4. while j > i, go to 1. 5. swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 59
  • 59.
    1. While data[i]<= data[p] ++i 2. While data[j] > data[p] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 5. Swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Shiwani Gupta 60
  • 60.
    1. While data[i]<= data[p] ++i 2. While data[j] > data[p] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 5. Swap data[j] and data[p] 2 4 10 12 13 50 57 63 100 p = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] > data[p] <= data[p] Shiwani Gupta 61
  • 61.
    Efficiency of Quicksort Basedon whether the partitioning is balanced. • Best case: split in the middle — Θ( n log n) – T(n) = 2T(n/2) + Θ(n) , n>1 //2 subproblems of size n/2 each – T(1)=0 • Worst case: sorted array — Θ( n2) – T(n) = T(n-1) + T(0) + Θ(n) //2 subproblems of size 0 and n-1 – T(0)= 0 • Average case: random arrays — Θ( n log n) //of size 9:1 or 99:1 62
  • 62.
  • 63.
  • 64.
    Solving Average caserecurrence log4/3 n levels = log2 n / log2 4/3 = log n T(n) = nlogn
  • 65.
    Improve Efficiency ofQuick Sort • Use mid element as pivot • Take the median as pivot • Take mean of first, last and middle element as pivot • Use random element as pivot – Running time is independent of input ordering – Worst case determined only by output of random-number generator Shiwani Gupta 66
  • 66.
    What is divideand conquer method? Show that algorithm for finding Minimum and Maximum using Divide and Conquer does not use more than 3n/2 comparisons Implement recursive Binary Search algorithm. Write complete code along with recursive function call Implement the Binary Search, prove that the complexity of binary search is O(log2n) Explain all sorting techniques based on divide and conquer strategy Sort the given elements using Merge Sort technique. 90, 20 80, 89, 70, 65, 85, 74. Show passes both for divide and combine Solve Merge sort recurrence using recursion tree method Give the analysis of Merge Sort using divide and conquer? Write Merge sort algorithm and analyze it Sort following data using Merge Sort 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 Give the analysis of Merge Sort using Divide and Conquer? Sort the given elements using Merge Sort technique. 90, 20 80, 89, 70, 65, 85, 74 Show how Quick sort can be made to run in O(nlogn) time in worst case Analyze worst and best case complexity for Quick sort Explain analysis of Quick Sort algorithm using recursion Sort following data using Quick Sort 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 Sort the given elements using Quick Sort technique. 90, 20 80, 89, 70,65, 85, 74 Prove that worst case complexity of Quick Sort is O(n2) Analyze worst and best case complexity for Quick Sort Task