DESIGN AND ANALYSIS OF ALGORITHM
Unit 1.2 Topics: General method- Binary Search- Finding the maximum and minimum- Merge sort- Quick Sort-
Selection- Strassen's matrix multiplication
Divide-And-Conquer- General Method:
Divide-and-Conquer Strategy breaks (divides) the given problem into sub problems, solve each sub
problems independently and finally conquer (combine) all sub problems solutions into whole solution.
Divide-and-Conquer Strategy suggests splitting the n inputs into k distinct subsets, 1< k < n, yielding k sub-
problems.
If the sub-problems are still relatively large, then the divide-and-conquer strategy can possibly be
reapplied.
Often the sub-problems resulting from a divide-and conquer design are of the same type as the original
problem.
Computing Time:
1
Binary Search:
A binary search algorithm is a technique for finding a particular value in a sorted list.
Divide-and-conquer can be used to solve this problem.
Any given problem P gets divided into one new sub-problem. This division takes only O(1) time.
After a comparison the instance remaining to be solved by using this divide-and-conquer scheme again.
If the element is found in the list successful search
If the element is not found in the list unsuccessful search
The Time Complexity of Binary Search is:
2
Recursive Binary Search- Algorithm
Iterative Binary Search- Algorithm
3
Binary Search Time Complexity
4
Finding the Maximum And Minimum:
The divide and- conquer technique is to find the maximum and minimum items in a set of n elements in
the given list.
5
Finding the Maximum And Minimum- Divide & Conquer:
If the list contains only one element, then
Maximum=Minimum= a[1] element only.
If the list contains two elements, then compare these two elements & find maximum and minimum.
If the list contains more than two elements, then divide the given list into sub lists based on middle
value.
Recursively perform this process until minimum & maximum value is found in the given list.
Time Complexity = O(n)
Algorithm:
6
7
Merge sort:
In Merge Sort, the elements are to be sorted in non decreasing order.
Given a sequence of n elements(also called keys) a[l],,.a[n]the general idea is to imagine them split into
two sets a[l]….a[n/2]and a[[n/2]+1]….a[n].
Each set is individually sorted, and the resulting sorted sequences are merged to produce a single sorted
sequence of n elements.
Merge sort Algorithm:
8
Merge- Algorithm:
Merge sort- Example
9
Merge sort- Time Complexity
10
Quick Sort:
In quick sort, the division into two sub arrays is made so that the sorted sub arrays do not need to be
merged later.
Three Steps involved here is:
1. Partitioning given array into sub arrays.
2. Interchanging two elements in the array.
3. Searching the input element.
Time Complexity = O(n*log n)
Quick Sort- Partitioning & Interchanging Algorithm:
11
Quick Sort Algorithm:
Quick sort- Example:
12
13
Quick sort- Time Complexity:
14
Selection:
Selection is used to find kth smallest element and place kth position in the given array of n elements say
a[1:n].
j is the position in which k element is there.
Here we are partitioning the given array into three parts (say k is small element):
1. Sub array which contains 1 to j-1 elements.
2. if k=j => element is found in jth position.
3. Sub array which contains (n-j) elements.
This partitioning process continues until we found kth smallest element.
Time Complexity= O(n2)
Selection- Partitioning & Interchanging Algorithm:
15
Selection- Algorithm
Selection- Example
16
17
Strassen's matrix multiplication:
18
Strassen's matrix multiplication- Formula:
Strassen's matrix multiplication- Example:
19
20
Strassen's matrix multiplication- Time Complexity:
*****************
21