Module 2 AAD
Module 2 AAD
Now the best case occurs when the elements are in increasing order. The
number of element comparisons is n – 1. The worst case occurs when the
elements are in decreasing order. In this case the number of element
comparisons is 2(n- 1). The average number of element comparisons is less than
2(n - 1).
Recursively finding the maximum and minimum
Note that 3n/2-2 is the best, average, and worst-case number of comparisons
when n is a power of two.
Compared with the 2n - 2 comparisons for the straightforward method, this is a
saving of 25% in comparison.
But ,in terms of storage, MaxMin is worse than the straightforward algorithm
because it requires stack space for i,j,max,min,max1, and min1.Given n
elements, there will be ⌊ log2n ⌋ +1 levels of recursion and we need to save
seven values for each recursive call.
Thus, MaxMin will be slower than StraightMaxMin because of the overhead of
stacking i,j,max,and min for the recursion
In divide and conquer technique if the assumption is not true, the
technique yields a less-efficient algorithm.Thus the divide-and-conquer
strategy is seen to be only a guide to better algorithm design which
may not always succeed.
BINARY SEARCH
In divide-and-conquer terminology, Binary Search locates a key x in a sorted
(nondecreasing order) array by first comparing x with the middle item of the
array.
If x equals the middle item, quit. Otherwise:
1. Divide the array into two subarrays about half as large. If x is smaller than the
middle item, choose the left subarray. If x is larger than the middle item, choose
the right subarray.
2. Conquer (solve) the subarray by determining whether x is in that subarray.
Unless the subarray is sufficiently small, use recursion to do this.
3. Obtain the solution to the array from the solution to the subarray.
(For Strassen’s Matrix Multiplication ,Quick Sort, Merge Sort refer Anoop sir’s
pdf)