2. WHAT IS DIVIDE AND CONQUER ALGORITHM ?
■ A divide and conquer algorithm is a strategy of solving a
large problem by
■ Breaking the problem into smaller sub-problems
■ Conquering the sub-problems, and
■ Combining them to get the desired output. PROBLEM(P)
P1
P2 Pn
……..
S1 S2 Sn
........
SOLUTION(S)
DIVIDE
CONQUER
COMBINE
3. .
How Divide and Conquer Algorithms Work?
Here are the steps involved:
DIVIDE : Divide the given problem into sub-problems using recursion.
CONQUER : Solve the smaller sub-problems recursively. If the subproblem is small
enough, then solve it directly.
COMBINE : Combine the solutions of the sub-problems that are part of the recursive
process to solve the actual problem
4. ALGORITHM FOR DIVIDE AND CONQUER
DAC(P)
{
if(small(P))
{
S(P);
}
else
{
divide P into P1,P2……..Pn
apply DAC(P1) ,DAC(P2),…..DAC(Pn)
combine(DAC(P1) ,DAC(P2),….DAC(Pn);
}
If(small(P)
FOR EX: (2+3)+(3-1)*(4+8)
(4+2)
(2+3)+ (3-1)*(4+8)
P1 P2 ….. Pn
5 + 2 * 12
=29
6. BINARY SEARCH
Binary Search is one of the fastest searching
algorithms.
ARRAY SHOULD BE SORTED IN ASCENDING
ORDER
It works on the principle of divide and conquer
technique.
*
*
*
8. 5 9 17 23 25 45 59 63 71 89
0 1 2 3 4 5 6 7 8 9
start end
KEY=59 #element to be found
m=10
EXAMPLE OF BINARY SEARCH
mid=start+end
2
=0+9/2
=4.5 => 4
So the mid value is 4
a[mid] = a[4] = 25
9. KEY= MID
KEY< MID -> END=MID-1
KEY>MID -> START=MID+1
And here now, as the key value is greater than the mid value,
Start = mid +1
= 4 +1
= 5
59>25
45 59 63 71 89
5 6 7 8 9
Start End
Mid = start + end = 5+9 = 14 = 7
2 2 2
a[mid]= 7 = 63
10. Here the key value is lesser than the mid value,
end = mid – 1
= 7 - 1
= 6
59<63
45 59
5 6
Start end
Mid= 5+6/2 = 11/2 = 5.5 = 5
a[mid]=45
11. Now the mid value is lesser than the key value,
Start= mid+ 1
= 5 + 1
= 6
45<59
59
6
Start end
Mid = 6 + 6
2
= 6 a[mid] = a[6] = 59
As key = mid
return mid
FINALLY THE KEY VALUE IS FOUND USING BINARY SEARCH
12. QUICK SORT
Quicksort is a divide-and-conquer algorithm.
It works by selecting a 'pivot' element from the
array and partitioning the other elements into two
sub-arrays, according to whether they are less than
or greater than the pivot.
For this reason, it is sometimes called partition-
exchange sort.
The key process in quick Sort is a partition()
*
*
*
*
13. THE RIGHT
SIDE MUST BE
GREATER
THAN PIVOT
THE LEFT
SIDE MUST BE
LESSER THAN
PIVOT
PIVOT CONDITION
LET US UNDERSTAND THROUGH AN EXAMPLE:
6 3 7 2 4 5
L R
PIVOT
CASE 1 :
Here the right side element is lesser than the pivot (ie)., P>R
P = 6 R = 5
In this condition, we need to swap the elements
6 3 7 2 4 5
14. 5 3 7 2 4 6
L P
R
Case 2:
Here, now compare the left side element and the pivot element
L=5 P=6 (P>L) # no swap
5 3 7 2 4 6
Case 3:
Here, as the left element is moved to the next element (ie)., 3
Compare the pivot element and the left element
L=3 P=6(P>L) # no swap
L
P
R
15. 5 3 7 2 4 6
L P
R
Case: 4
Now, the left element is moved to the next element, compare the element
with the pivot element…
L=7 P=6 (P<L)
In this condition, we need to swap.
5 3 6 2 4 7
P R
L
Case: 5
As the elements were swapped, now compare the pivot element with
the right side element…
P= 6 R=7 (P<R)
Here the pivot element is lesser than the right
Side, hence no swap
16. 5 3 6 2 4 7
Case:6
Here the pivot element is greater than the
right side element P>R
P=6 R=4 #SWAP
P R
L
Case: 7
Now the swapping takes place, let’s compare
the pivot element with the left side element
P=6 L=4 (P>L) #no swap
5 3 4 2 6 7
P
L
R
17. Case:8
Here the pivot is greater than the left side
element
P=6 L=2 #no swap
FINALLY, We get the array
L P
R
Apply the quick sort on the rounded parts
18. 5 3 4 2
P L
R
In this array, the pivot element is taken as 5, and the right element is 2
P=5 R= 2 (P>R) # Swap
2 3 4 5
P R
L
2<5
3<5
4<5
Now merge all the arrays
20. MERGE SORT
The divide-and-conquer algorithm breaks down a big
problem into smaller, more manageable pieces that
look similar to the initial problem.
It then solves these subproblems recursively and puts
their solutions together to solve the original problem.
Merge sort is defined as a sorting algorithm that works
by dividing an array into smaller subarrays, sorting
each subarray, and then merging the sorted subarrays
back together to form the final sorted array.
*
*
*
24. 1 5 8 15 24 3 10 16 20
LEFT SUB ARRAY RIGHT SUB ARRAY
Compare the values on the left sub array and then the right sub array
Compare the values and merge them.
1 3 5 8 10 15 16 20 24
And through this, you will find the sorted array for merge sort