Devide and Conqure Rule
Devide and Conqure Rule
Like dynamic and greedy methods, Divide and Conquer is an algorithmic paradigm. A
typical Divide and Conquer algorithm solves a problem using following three steps.
Merge Sort
Quick Sort
Binary Search
Binary search
15 27 33 83 92 99
Pass 1
15 27 33 83 92 99
| Check for 99
Pass 2
15 27 33 83 92 99
| Check for 99
Pass 3
15 27 33 83 92 99
| finds 99
If (arr[mid] == x)
Return mid;
If (arr[mid] > x)
Return Binary Search (arr, l, mid - 1, x);
Return -1;
}
Or
Let say the iteration in Binary Search terminates after k iterations. In the above example,
it terminates after 3 iterations, so here k = 3
At each iteration, the array is divided by half. So let’s say the length of array at any
iteration is n.
At Iteration 1,
Length of array = n
At Iteration 2,
Length of array = n⁄2
At Iteration 3,
Length of array = (n⁄2)⁄2 = n⁄22
Therefore, after Iteration k,
Length of array = n⁄2k
Also, we know that after
After k divisions, the length of array becomes 1
Therefore
Length of array = n⁄2k = 1
=> n = 2k
Applying log function on both sides:
=> log2 (n) = log2 (2k)
=> log2 (n) = k log2 (2)
As (loga (a) = 1)
Therefore,
=> k = log2 (n)
Hence the time complexzity of Binary Search is
log2 (n)
Merge sort
Let's consider an array with values {14, 7, 3, 12, 9, 11, 6, and 12}
Below, we have a pictorial representation of how merge sort will sort the given array.
In merge sort we follow the following steps:
1. We take a variable p and store the starting index of our array in this. And we
take another variable r and store the last index of array in it.
2. Then we find the middle of the array using the formula (p + r)/2 and mark
the middle index as q, and break the array into two subarrays, from p to q and
from q + 1 to r index.
3. Then we divide these 2 subarrays again, just like we divided our main array
and this continues.
4. Once we have divided the main array into subarrays with single elements,
then we start merging the subarrays.
2. Then, we make two recursive calls to Merge Sort, with arrays of sizes ⌊(n − 1)/2⌋
and ⌈(n − 1)/2⌉.
3. Finally, we call Merge. Merge goes through the two subarrays with one loop, always
increasing one of i and j.
Thus we have:
(1) T(1) = 1
(7) ……
(8) T(2) / 2 = T(1) / 1 + 1
smallest) item.
= (n − 1) + (n − 2) +. . .+ 2 + 1 − 0
T(n)= (n − 1) + (n − 2) +. . .+ 2 + 1 =(n−1)n/2
Best case
T(n) = partition(n) + 2*T(n/2) // partition(n) = n
= n + 2*T(n/2)
= 2*T(n/2) + n
= 24*T(n/16) + 4n
and so on....
= 2k*T(1) + k*n
= 2k*1 + k*n
= 2k + k*n // n = 2k
= n + k*n
= n + (lg(n))*n
= n*( lg(n) + 1 )
~= n*lg(n))
Average case:
Let T(n) denotes the average time required to sort an array of n elements. A call
to RANDOMIZED_QUICKSORT with a 1 element array takes a constant time, so
we have T(1) = (1).
After the split RANDOMIZED_QUICKSORT calls itself to sort two subarrays.
The average time to sort an array A[1 . . q] is T[q] and the average time to sort
an array A[q+1 . . n] is T[n-q]. We have
T(n) = 1/n (T(1) + T(n-1) + n-1∑q=1 T(q) + T(n-q))) +
(n) ----- 1
We know from worst-case analysis
If we can pick 'a' and 'b' large enough so that n lg n + b > T(1). Then for n >
1, we have
T(n) ≥ n-1∑k=1 2/n (aklgk + b) + (n)
= 2a/n n-1∑k=1 klgk - 1/8(n2) + 2b/n (n -1) + (n) -------
4
At this point we are claiming that
n-1
∑k=1 klgk ≤ 1/2 n2 lgn - 1/8(n2)
Stick this claim in the equation 4 above and we get