DAA Module-2
DAA Module-2
• Divide-and-Conquer:
– If n=1 terminate (every one-element list is already sorted)
– If n>1, partition elements into two; based on pivot
element
• Prtition, a situation where all the elements
before some position s are smaller than or
equal to A[s] and all the elements after
positions are greater than or equal to A[s]:
• Since we want elements smaller than the pivot to
be in the first part of the subarray, this scan skips
over elements that are smaller than the pivot and
stops on encountering the first element greater
than or equal to the pivot.
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43
43 15
15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43
43 15 9 1 18 26 32 6 43 15
15 9 1
18 26 32 6 43 15 9 1
Max Min
Problem statement
• Given a list of n elements, the problem is to find the
maximum and minimum items.
A simple and straight forward algorithm to achieve
this is given below.
Binary Search
• Problem definition:
• Let ai, 1 ≤ i ≤ n be a list of elements that are sorted in
non-decreasing order.
• The problem is to find whether a given element x is
present in the list or not.
– If x is present we have to determine a value j (element’s
position) such that aj=x.
– If x is not in the list, then j is set to zero.
• Solution:
• Let P = (n, ai…al , x) denote an arbitrary
instance of search problem
• - where n is the number of elements in the
list,
• - ai…al is the list of elements and
• - x is the key element to be searched
Binary Search
• Pseudocode
• Step 1: Pick an index q in the middle range [i, l] i.e. q = (n +1)/2
and compare x with aq.
• Step 2: if x = aq i.e key element is equal to mid element, the
problem is immediately solved.
• Step 3: if x < aq in this case x has to be searched for only
in the sub-list ai, ai+1, ……, aq-1. Therefore problem reduces to (q-
i, ai…aq-1, x).
• Step 4: if x > aq , x has to be searched for only in the
sub-list aq+1, ...,., al . Therefore problem reduces to (l-i,
aq+1…al, x).
Analysis
• Time Complexity
Recurrence relation (for worst case) T(n) = T(n/2) + c
STRASSEN'SMATRIXMULTIPLICATION
Matrix Multiplication
Direct Method:
• Suppose we want to multiply two n x n matrices, A
and B.
• Their product, C=AB, will be an n by n matrix and will
therefore have n2 elements.
• The number of multiplications involved in producing
the product in this way is Θ(n3)
• Divide and Conquer method for Matrix multiplication