Module II - Daa
Module II - Daa
1
technique, can run on the multiprocessor system or in different
machines simultaneously.
In this approach, most of the algorithms are designed using recursion,
hence memory management is very high. For recursive function stack
is used, where function state needs to be stored.
Following are some problems, which are solved using divide and conquer
approach.
Finding the maximum and minimum of a sequence of numbers
Strassen’s matrix multiplication
Merge sort
Binary search
If the sizes of the two sub problems are approximately equal then the
computing time of DANDC is:
2
Binary Search
3. If the Pivot Element (the item to be searched) is less than the item in the
middle of the interval, We discard the second half of the list and recursively
repeat the process for the first half of the list by calculating the new middle
and last element.
4. If the Pivot Element (the item to be searched) is greater than the item in
the middle of the interval, we discard the first half of the list and work
recursively on the second half by calculating the new beginning and middle
element.
Analysis:
1. Input: an array A of size n, already sorted in the ascending or
descending order.
2. Output: analyze to search an element item in the sorted array of size
n.
3. Logic: Let T (n) = number of comparisons of an item with n elements
in a sorted array.
3
o Set low = 1 and high = n
o Find mid =(low+high)/2
o Compare the search item with the mid item.
It eliminates half of the list from further searching by using the result of
each comparison.
It indicates whether the element being searched is before or after the
current position in the list.
This information is used to narrow the search.
For large lists of data, it works significantly better than linear search.
4
5
Time Complexity Analysis-
Binary Search time complexity analysis is done below-
In each iteration or in each recursive call, the search gets reduced to half
of the array.
So for n elements in the array, there are log 2n iterations or recursive calls.
Thus, we have-
Time Complexity of a successful Binary Search Algorithm is O(log2n) and for an
unsuccessful search is Θ(log n).Here, n is the number of elements in the sorted linea
array.
6
3. By comparing numbers of elements, the time complexity of this
algorithm can be analyzed.
4. Hence, the time is determined mainly by the total cost of the element
comparison.
Explanation:
a. Straight MaxMin requires 2(n-1) element comparisons in the best, average
& worst cases.
b. By realizing the comparison of a [i]max is false, improvement in a
algorithm can be done.
c. Hence we can replace the contents of the for loop by, If (a [i]> Max) then
Max = a [i]; Else if (a [i]< 2(n-1)
d. On the average a[i] is > max half the time, and so, the avg. no. of
comparison is 3n/2-1.
7
Algorithm:
Example:
8
A 1 2 3 4 5 6 7 8 9
Values 22 13 -5 -8 15 60 17 31 47