Professional Documents
Culture Documents
7 Divide and Conquer
7 Divide and Conquer
2
BITS Pilani, Pilani Campus
What is Divide and Conquer?
3
BITS Pilani, Pilani Campus
Defective Chess Board
What is it?
Normal Square
Defective Square
5
BITS Pilani, Pilani Campus
Defective Chess Board
Some Mathematics
x =1
x=0
9
BITS Pilani, Pilani Campus
Defective Chess Board
Time Complexity – Through Recurrence Equation
T(k) = 4T(k-1) + c
= 4[4T(k-2) + c] + c
= 42 T(k-2) + 4c + c
= 42[4T(k-3) + c] + 4c + c
= 43 T(k-3) + 42c + 4c + c
=…
= 4k T(0) + 4k-1c + 4k-2c + ... + 42c + 4c + c for kth term
= 4k d + 4k-1c + 4k-2c + ... + 42c + 4c + c T(0) = d
= O(4k) = O (2kx2k)
= O (number of squares)
10
BITS Pilani, Pilani Campus
Binary Search
Divide and Conquer Approach
0 1 2 3 4 5 6 7 8
list = 10 20 30 40 50 60 70 80 90
low = 0 mid=(low + high)/2 = (0+8)/2 = 4 high= 8
1. In the beginning elements at low, mid and high indices are known in the sorted list.
2. If the key is to be searched = 50.
o Since key = list[mid], search terminates here.
3. If the key is to be searched = 70 (> 50 the mid element)
o New low = mid+1 = 4+1 = 5
o New high = high = 8 3 independent
o New mid = (low + high)/2 = (5+8)/2 = 6 cases.
4. If the key is to be searched = 30 (< 50 the mid element)
o New low = low = 0
o New high = mid-1 = 4-1 = 3
o New mid = (low + high)/2 = (0+3)/2 = 1
5. The steps 1, 2, and 3 are repeated as long as low <= high or the key is found.
11
BITS Pilani, Pilani Campus
Binary Search
Examples
0 1 2 3 4 5 6 7 8
10 20 30 40 50 60 70 80 90
low = 0 mid=(low + high)/2 = (0+8)/2 = 4 high= 8
12
BITS Pilani, Pilani Campus
Binary Search
Recurrence Equation & Time Complexity
T(n) = 1 + T(n/2)
T(n/2) = 1 + T(n/4)
T(n/4) = 1 + T(n/8)
T(n/8) = 1 + T(n/16) Illustration:
Taking only last 4 expressions and adding on both the sides:
. T(8) + T(4) + T(2) + T(1) = 1+1+1+1 + T4 + T(2) + T(1), or
T(8) = 1 + T(4) T(8) = 1 + 1 + 1 + 1, or
T(8) = 1+ 3, or
T(4) = 1 + T(2) T(8) = 1 + log28
T(2) = 1 + T(1) Generally we can observe that:
T(1) =1 T(n) = 1 + log2n
Adding both sides,
T (n) = 1 + log2n
T(n) = O(log2n)
13
BITS Pilani, Pilani Campus
Exercise
14
BITS Pilani, Pilani Campus
Merge Sort
Divide and Conquer Approach
15
BITS Pilani, Pilani Campus
Merge Sort
Illustration
0 1 2 3 4 5 6
low = 0 38 27 43 3 9 82 10 high= 6
0 1 2 3 4 5 6
38 27 43 3 9 82 10
0 1 2 3 4 5 6
38 27 43 3 9 82 10
0 1 2 3 4 5 6
38 27 43 3 9 82 10
0 1 2 3 4 5 6
27 38 3 43 9 82 10
0 1 2 3 4 5 6
3 27 38 43 9 10 82
All of these are 0 1 2 3 4 5 6
Element movements in
the same array. 3 9 10 27 38 43 82
BITS Pilani, Pilani Campus
Merge Sort
Pseudo Code
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
i= low mid j= mid+1 high
0 1 2 3 4 5 6
tempList 3
k
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
low i mid j = mid+1 high
0 1 2 3 4 5 6
tempList 3 9
k
19
BITS Pilani, Pilani Campus
Merge Sort
How Merge Works continues...
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
low i mid mid+1 j high
0 1 2 3 4 5 6
tempList 3 9 10
k
20
BITS Pilani, Pilani Campus
Merge Sort
How Merge Works continues...
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
low i mid mid+1 j= high
0 1 2 3 4 5 6
tempList 3 9 10 27
k
21
BITS Pilani, Pilani Campus
Merge Sort
How Merge Works continues...
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
low i mid mid+1 j= high
0 1 2 3 4 5 6
tempList 3 9 10 27 38
k
22
BITS Pilani, Pilani Campus
Merge Sort
How Merge Works continues...
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
low i = mid mid+1 j= high
0 1 2 3 4 5 6
tempList 3 9 10 27 38 43
k
23
BITS Pilani, Pilani Campus
Merge Sort
How Merge Works concluded.
0 1 2 3 4 5 6
list 3 27 38 43 9 10 82
low i = mid mid+1 j= high
0 1 2 3 4 5 6
tempList 3 9 10 27 38 43 82
k
24
BITS Pilani, Pilani Campus
Merge Sort
Merge - Pseudo Code
Algorithm merge(list, low, mid, high)
{
i = low; j = mid+1; k = low;
tempList is a local variable list for this function;
while (i <= mid AND j <= high) {
if (list[i] < list[j]) {
tempList[k] = list[i]; i = i+1; k = k+1;
}
else {
tempList[k] = list[j]; j = j+1; k = k+1;
}
}
// Copy remaining elements of two parts – if there are any
while (i <= mid) {
tempList[k] = list[i]; k= k+1; i = i+1;
}
while (j <= high) {
tempList[k] = list[j]; k= k+1; j =j+1;
}
// Copy all elements from tempList to list – because tempList does not exist outside this function (local)
for (i = low to high) {
list[i] = tempList[i];
}
} 25
BITS Pilani, Pilani Campus
Exercise
26
BITS Pilani, Pilani Campus
Merge Sort
Recurrence Equation & Time Complexity
// Terminal condition will be when only one element left in the partition
// Please note – Quick Sort Algorithm does not suggest how to select the pivot.
quickSort (list, low, pivotIndex-1); // recursive call for the left segment
quickSort (list, pivotIndex+1, high); // recursive call for the right segment
// pivotIndex = the array index of the pivot
}
28
BITS Pilani, Pilani Campus
Partitioning Example
Quick Sort
pivot = 44 0 1 2 3 4 5 6 7 8
low 44 75 23 43 55 12 64 77 33 high
up down
0 1 2 3 4 5 6 7 8
low 44 75 23 43 55 12 64 77 33 high
up down
Value on the left segment that Value on the right segment
is larger than the pivot. that is smaller than the pivot.
0 1 2 3 4 5 6 7 8
low 44 33 23 43 55 12 64 77 75 high
up down
0 1 2 3 4 5 6 7 8
low 44 33 23 43 55 12 64 77 75 high
up down
0 1 2 3 4 5 6 7 8
low 44 33 23 43 55 12 64 77 75 high
up down
30
BITS Pilani, Pilani Campus
Example Continues..
pivot = 44 0 1 2 3 4 5 6 7 8
low 44 33 23 43 55 12 64 77 75 high
0 1 2 3 4 5 6 7 8
low 44 33 23 43 12 55 64 77 75 high
up down
31
BITS Pilani, Pilani Campus
Example Concluded.
pivot = 44
0 1 2 3 4 5 6 7 8
low 44 33 23 43 12 55 64 77 75 high
0 1 2 3 4 5 6 7 8
low 12 33 23 43 44 55 64 77 75 high
34
BITS Pilani, Pilani Campus
Interesting Reading
35
BITS Pilani, Pilani Campus
Appendix
Quick Sort Partitioning Examples
36
BITS Pilani, Pilani Campus
Partitioning Example
Quick Sort
38
BITS Pilani, Pilani Campus
Partitioning Example
Quick Sort
39
BITS Pilani, Pilani Campus
Thank You
40
BITS Pilani, Pilani Campus