Divide and Conquer
Divide-and-conquer
Divide-and-conquer is probably the best-known general algorithm
design technique.
Divide-and-conquer algorithms work according to the following
general plan:
A problem is divided into several sub problems of the same type,
ideally of about equal size.
Recursively solve the sub problems
The solutions to the sub problems are combined to get a solution
to the original problem.
Each problem of size n is divided into a sub-problems of size n/b.
Divide-and-conquer algorithms often follow a generic pattern
They tackle a problem of size n by recursively solving
“a” subproblems of size “n/b” and
then combining these answers in O(nd ) time
for some a, b, d > 0
Assuming that size n is a power of b to simplify our analysis, we get
the following recurrence for the running time T (n):
Merge Sort
Mergesort is a perfect example of a successful application of the
divide-and conquer technique.
It sorts a given array A[0..n − 1] by dividing it into two halves
A[0..n/2 − 1] and A[n/2..n − 1], sorting each of them recursively,
and
then
Merging the two smaller sorted arrays into a single sorted one.
Assuming for simplicity that n is a power of 2, the recurrence
relation for the number of key comparisons C(n) is
Cmerge(n), the number of key comparisons performed during the merging stage.
At each step, exactly one comparison is made, after which the
total number of elements in the two arrays still needing to be processed is
reduced by 1.
In the worst case, neither of the two arrays becomes empty before the other one
contains just one element
Therefore, for the worst case, Cmerge(n) = n − 1, and we have the
recurrence
Question
Analyze the time complexity to sort an array using Merge Sort in its
worst case.
Quick Sort
QuickSort is a sorting algorithm based on the Divide and
Conquer algorithm
That picks an element as a pivot and partitions the given
array around the picked pivot by placing the pivot in its
correct position in the sorted array.
The number of key comparisons made before a partition is achieved is
n + 1 if the scanning indices cross over and n if they coincide
Best Case : If all the splits happen in the middle of corresponding
subarrays, we will have the best case. The number of key comparisons in
the best case satisfies the recurrence
As Per Master’s Theorem
Cbest(n) = n log n.
2
Worst Case
In the worst case, all the splits will be skewed to the extreme: one of
the two subarrays will be empty, and the size of the other will be just
1 less than the size of the subarray being partitioned.
This unfortunate situation will happen, in particular, for increasing
arrays, i.e., for inputs for which the problem is already solved!
Indeed, if A[0..n − 1] is a strictly increasing array and we use A[0] as
the pivot, the left-to-right scan will stop on A[1] while the right-to-
left scan will go all the way to reach A[0], indicating the split at
position 0
Question
Write quick sort algorithm to sort an array. Find its time complexity.
Apply the algorithm to sort the following list of elements.
{55, 28, 35, 78, 110, 48, 88}
Questions
Apply Stressen’s matrix multiplication to multiply the following two
matrices: