Professional Documents
Culture Documents
Rutal Mahajan
S.N.P.I.T.R.C., Umrakh
Rutal.mahajan@gmail.com
14 23 45 98 6 33 42 67
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 29
14 23 45 98 6 33 42 67
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 30
14 23 45 98 6 33 42 67
6 14
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 31
14 23 45 98 6 33 42 67
6 14 23
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 32
14 23 45 98 6 33 42 67
6 14 23 33
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 33
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 34
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 35
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 36
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
02-Oct-17 Divide and Conquer (DAA B.E.5th semester) by Rutal Mahajan 37
• Example 3: given two sorted sub arrays
L1 = { 3 10 23 54 } L2 = { 1 5 25 75}
merge(L1, L2) = { 1 3 5 10 23 25 54 75}
Merge sort
X: Y:
3 10 23 54 1 5 25 75
Result:
X: Y:
3 10 23 54 5 25 75
Result:
1
X: Y:
10 23 54 5 25 75
Result:
1 3
X: Y:
10 23 54 25 75
Result:
1 3 5
X: Y:
23 54 25 75
Result:
1 3 5 10
X: Y:
54 25 75
Result:
1 3 5 10 23
X: Y:
54 75
Result:
1 3 5 10 23 25
X: Y:
75
Result:
1 3 5 10 23 25 54
X: Y:
Result:
1 3 5 10 23 25 54 75
If we look at the Merge Sort algorithm (from last class) more closely, we
notice that the running time on an array of size n accrues as follows:
Conquer:
Sort the two subarrays A[p...q-1] and A[q+1...r] by recursive calls to quick sort.
Combine:
Since the sub arrays are sorted in place, no work is needed to combine them: entire
array A[p...r] is sorted now.
….
Disadvantages:
1. not stable when two elements are same
2. O(n2) time for worst case sorting
3. Efficient in practice
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 80 60 50 7 30 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 60 50 7 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 0 40 20 10 30 7 50 60 80 100
i j
pivot_index = 4 7 20 10 30 40 50 60 80 100
i j
7 20 10 30 40 50 60 80 100
7 20 10 30 40 50 60 80 100
pivot_index = 0 2 4 10 12 13 50 57 63 100
i j
i j
i j
i j
1. (5 3 1 9 8 2 4 7) take 5 as a key
2. (36 15 40 1 60 20 55 25 50 20)
hint. take key =A[10] i.e 20
B n- bit number
Then…
XY = (x1Bm + x0) y1Bm + y0
= x1 y1B2m + (x0 y1 + x1 y0)Bm + x0 y0
Run time…
T(n) = 4T (n/2) +O(n)
X= x1Bm + x0
Y= y1Bm + y0
Then…
XY = (x1Bm + x0) y1Bm + y0
= x1 y1B2m + (x0 y1 + x1 y0)Bm + x0 y0
= Z2 B2m + Z1(x0 y1 + x1 y0)Bm + Z0
Z2 = x1 y1
Z1 = x1 y0 + x0 y1
Z0 = x0 y0
Run time…
T(n) = 3T (n/2) +O(n)
So after solving it we get, T(n) = O(nlog3)
02-Oct-17 Sorting Algorithms(DAA B.E.5th semester) by Rutal Mahajan 105
Strassen’s matrix multiplication
algorithm
N
Ci , j ai ,k bk , j
Time analysis from k 1
code: N N N
Thus T ( N ) c cN 3 O( N 3 )
i 1 j 1 k 1
A B = R
A0 A1 B0 B1 A0B0+A1B2 A0B1+A1B3
=
A2 A3 B2 B3 A2B0+A3B2 A2B1+A3B3
a0 b0 = a0 b0