2. Divide and Conquer Algorithms
Top-down technique for designing
algorithms.
Divides the problem into smaller sub
problems and then composes the partial
solutions into the solutions of the original
problem
4. Divide and Conquer
Algorithms based on Divide and Conquer:
Merge Sort
Quick Sort
Binary Search
Tower of Hanoi
5. Merge sort
IDEA: Break down a list into several sub-lists
until each sub list consists of a single element
and merge those sub lists in a manner that
results into a sorted list.
8. Quick sort
Doesn’t use additional storage
Selects a pivot value to assist with splitting the
array
The actual position where the pivot value
belongs in the final sorted array, commonly
called as split point , will be used to divide the
array for subsequent calls to the quick sort .
9. Quick Sort
Different versions:
Always pick first element as pivot.
Always pick last element as pivot.
Pick a random element as pivot.
Pick median as pivot.
10. Quick sort
Step 1 - Choose the first index value has pivot.
Step 2 − Take two variables to point left and right of the array.
Step 3 − left points to the low index.
Step 4 − right points to the high index.
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
13. Binary Search
Works on sorted array.
Uses the middle array to compare.
2 3 6 8 10 12 15
MIDDLE
1. Check if the Middle number is equal to our number
True Terminate
False Continue
2. Check if our number is less than the middle no
True
2 3 6
MIDDLE
Continue from Step 1 with
this array
14. False Continue
3. Check if our number is greater than the middle no
True
10 12 15
MIDDLE
False “Element not there in the array ”
Continue from Step 1
with this array
16. Tower of Hanoi
A mathematical puzzle where we have three
rods and n disks.
Objective:
To move the entire stack to another rod
Rules:
1) Only one disk can be moved at a time.
2) a disk can only be moved if it is the
uppermost disk on a stack.
3) No disk may be placed on top of a smaller
disk.
1.In a perfect world, where the problem is easy to divide, and the sub-problem at some level is easy to solve, divide and conquer can be optimal for a general case solution, like merge sort.
2. Parallel availability, divide and conquer by it’s very nature lends itself well to parallel processing.
3. Memory Access: Each problem is divided into various sub-problems. Thus, the size of each sub-problem becomes extremely small and can be stored and processed in the cache memory. It requires 0 or extremely small need of using main memory.
1.Problem decomposition may be very complex and thus not really suitable to divide and conquer.
2.Recursive nature of the solution may end up duplicating sub-problems, dynamic/memoized solutions may be better in some of these cases, like Fibonacci.
3. Recursion into small/tiny base cases may lead to huge recursive stacks, and efficiency can be lost by not applying solutions earlier for larger base cases.