SlideShare ist ein Scribd-Unternehmen logo
1 von 66
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie module2_dIVIDEncONQUER_2022.pdf

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
lecture 15
lecture 15lecture 15
lecture 15
sajinsc
 
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfQuicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
anupamfootwear
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 

Ähnlich wie module2_dIVIDEncONQUER_2022.pdf (20)

Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
2.pptx
2.pptx2.pptx
2.pptx
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
03 dc
03 dc03 dc
03 dc
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
lecture 15
lecture 15lecture 15
lecture 15
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfQuicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Merge sort
Merge sortMerge sort
Merge sort
 
lecture 10
lecture 10lecture 10
lecture 10
 
Admission in india 2015
Admission in india 2015Admission in india 2015
Admission in india 2015
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 

Mehr von Shiwani Gupta

Mehr von Shiwani Gupta (20)

ML MODULE 6.pdf
ML MODULE 6.pdfML MODULE 6.pdf
ML MODULE 6.pdf
 
ML MODULE 5.pdf
ML MODULE 5.pdfML MODULE 5.pdf
ML MODULE 5.pdf
 
ML MODULE 4.pdf
ML MODULE 4.pdfML MODULE 4.pdf
ML MODULE 4.pdf
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdf
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdfmodule1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
 
ML MODULE 1_slideshare.pdf
ML MODULE 1_slideshare.pdfML MODULE 1_slideshare.pdf
ML MODULE 1_slideshare.pdf
 
ML MODULE 2.pdf
ML MODULE 2.pdfML MODULE 2.pdf
ML MODULE 2.pdf
 
ML Module 3.pdf
ML Module 3.pdfML Module 3.pdf
ML Module 3.pdf
 
Problem formulation
Problem formulationProblem formulation
Problem formulation
 
Simplex method
Simplex methodSimplex method
Simplex method
 
Functionsandpigeonholeprinciple
FunctionsandpigeonholeprincipleFunctionsandpigeonholeprinciple
Functionsandpigeonholeprinciple
 
Relations
RelationsRelations
Relations
 
Logic
LogicLogic
Logic
 
Set theory
Set theorySet theory
Set theory
 
Uncertain knowledge and reasoning
Uncertain knowledge and reasoningUncertain knowledge and reasoning
Uncertain knowledge and reasoning
 
Introduction to ai
Introduction to aiIntroduction to ai
Introduction to ai
 
Planning Agent
Planning AgentPlanning Agent
Planning Agent
 

KĂźrzlich hochgeladen

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

KĂźrzlich hochgeladen (20)

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

module2_dIVIDEncONQUER_2022.pdf

  • 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 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
  • 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 by substitution 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 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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 given array of n integers to sort: 40 20 10 80 60 50 7 30 100 Shiwani Gupta 26
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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. 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
  • 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 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
  • 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 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
  • 62. Solving Worst case recurrence
  • 63. Solving Best case recurrence
  • 64. Solving Average case recurrence log4/3 n levels = log2 n / log2 4/3 = log n T(n) = nlogn
  • 65. 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
  • 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