DAA Unit II
DAA Unit II
DAA Unit II
Blog: anilkumarprathipati.wordpress.com 1
UNIT-2 DIVIDE AND CONQUER
Binary Search:
Binary search is an efficient searching technique that works with only sorted lists. So the
list must be sorted before using the binary search method. Binary search is based on divide-and-
conquer technique.
The process of binary search is as follows:
The method starts with looking at the middle element of the list. If it matches with the key
element, then search is complete. Otherwise, the key element may be in the first half or second
half of the list. If the key element is less than the middle element, then the search continues with
the first half of the list. If the key element is greater than the middle element, then the search
continues with the second half of the list. This process continues until the key element is found
or the search fails indicating that the key is not there in the list.
Consider the list of elements: -4, -1, 0, 5, 10, 18, 32, 33, 98, 147, 154, 198, 250, 500.
Trace the binary search algorithm searching for the element -1.
Blog: anilkumarprathipati.wordpress.com 2
UNIT-2 DIVIDE AND CONQUER
Sol: The given list of elements are:
Low High
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Blog: anilkumarprathipati.wordpress.com 3
UNIT-2 DIVIDE AND CONQUER
The following algorithm gives the iterative binary Search Algorithm
Algorithm BinarySearch(a, n, key)
{
// a is an array of size n elements
// key is the element to be searched
// if key is found in array a, then return j, such that
//key = a[i]
//otherwise return -1.
Low: = 0;
High: = n-1;
While (low high) do
{
Mid: = (low + high)/2;
If ( key = a[mid]) then
Return mid;
Else if (key < a[mid])
{
High: = mid +1;
}
Else if( key > a[mid])
{
Low: = mid +1;
}
}
The following algorithm gives Recursive Binary Search
Algorithms Binsearch ( a, n, key, low, high)
{
// a is array of size n
// Key is the element to be searched
// if key is found then return j, such that key = a[i].
//otherwise return -1
If ( low high) then
{
Mid: = (low + high)/2;
If ( key = a[mid]) then
Return mid;
Else if (key < a[mid])
Binsearch ( a, n, key, low, mid-1);
Else if ( key > a[mid])
Binsearch ( a, n, key, mid+1, high);
}
Return -1;
}
Advantages of Binary Search: The main advantage of binary search is that it is faster than
sequential (linear) search. Because it takes fewer comparisons, to determine whether the given
key is in the list, then the linear search method.
Blog: anilkumarprathipati.wordpress.com 4
UNIT-2 DIVIDE AND CONQUER
Disadvantages of Binary Search: The disadvantage of binary search is that can be applied to
only a sorted list of elements. The binary search is unsuccessful if the list is unsorted.
Efficiency of Binary Search: To evaluate binary search, count the number of comparisons in
the best case, average case, and worst case.
Best Case: The best case occurs if the middle element happens to be the key element. Then only
one comparison is needed to find it. Thus the efficiency of binary search is O(1).
Ex: Let the given list is: 1, 5, 10, 11, 12.
Low Mid High
1 5 10 11 12
Let key = 10.
Since the key is the middle element and is found at our first attempt.
Worst Case: Assume that in worst case, the key element is not there in the list. So the process of
divides the list in half continues until there is only one item left to check.
Items left to search Comparisons so far
16 0
8 1
4 2
2 3
1 4
For a list of size 16, there are 4 comparisons to reach a list of size one, given that there is one
comparison for each division, and each division splits the list size in half.
In general, if n is the size of the list and c is the number of comparisons, then
C = log2 n
.
. . Eficiency in worst case = O(log n)
Average Case: In binary search, the average case efficiency is near to the worst case efficiency.
So the average case efficiency will be taken as O(log n).
Efficiency in average case = O (log n).
Binary Search
The quick sort is considered to be a fast method to sort the elements. It was developed by
CAR Hoare. This method is based on divide-and-conquer technique i.e. the entire list is divided
into various partitions and sorting is applied again and again on these partitions. This method is
also called as partition exchange sorts.
The quick sort can be illustrated by the following example
12 6 18 4 9 8 2 15
Blog: anilkumarprathipati.wordpress.com 5
UNIT-2 DIVIDE AND CONQUER
The reduction step of the quick sort algorithm finds the final position of one of the
numbers. In this example, we use the first number, 12, which is called the pivot (rotate) element.
This is accomplished as follows-
Let ‘i’ be the position of the second element and ‘j’ be the position of the last element.
i.e. i =2 and j =8, in this example.
Assume that a [n+1] =, where ‘a’ is an array of size n.
[1] [2] [3] [4] [5] [6] [7] [8] [9] i j
12 6 18 4 9 8 2 15 2 8
First scan the list from left to right (from i to j) can compare each and every element with
the pivot. This process continues until an element found which is greater than or equal to pivot
element. If such an element found, then that element position becomes the value of ‘i’.
Now scan the list from right to left (from j to i) and compare each and every element with
the pivot. This process continues until an element found which is less than or equal to pivot
element. If such an element finds then that element’s position become ‘j’ value.
Now compare ‘i’ and ‘j’. If i <j, then swap a[i] and a[j]. Otherwise swap pivot element
and a[j].
Continue the above process the entire list is sorted.
[1] [2] [3] [4] [5] [6] [7] [8] [9] i j
12 6 18 4 9 8 2 15 2 8
12 6 18 4 9 8 2 15 3 7
12 6 2 4 9 8 18 15 7 6
Since i = 7 j=6, then swap pivot element and 6th element ( jth element), we get
8 6 2 4 9 12 18 15
Thus pivot reaches its original position. The elements on left to the right pivot are smaller
than pivot (12) and right to pivot are greater pivot (12).
8 6 2 4 9 12 18 15
Sublist 1 Sublist 2
Now take sub-list1 and sub-list2 and apply the above process recursively, at last we get
sorted list.
Ex 2: Let the given list is-
8 18 56 34 9 92 6 2 64
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] i j
8 18 56 34 9 92 6 2 64 2 98
8 18 56 34 9 92 6 2 64 2 8
8 2 56 34 9 92 6 18 64 3 7
8 2 6 34 9 92 56 18 64 4 3
6 2 8 34 9 92 56 18 64
< > < >
Sublist 1 Sublist 2
Blog: anilkumarprathipati.wordpress.com 6
UNIT-2 DIVIDE AND CONQUER
Now take a sub-list that has more than one element and follow the same process as
above. At last, we get the sorted list that is, we get
2 6 8 9 18 34 56 64 92
The following algorithm shows the quick sort algorithm-
Algorithm Quicksort(i, j)
{
// sorts the array from a[i] through a[j]
If ( i <j) then //if there are more than one element
{
//divide P into two sub-programs
K: = partition (a, i, j+1);
//Here K denotes the position of the partitioning element
//solve the sub problems
Quicksort(i, K-1);
Quicksort(K=1, j);
// There is no need for combining solution
}
}
Algorithm Partition (a, left, right)
{
// The element from a[left] through a[right] are rearranged in such a manner that if initially
// pivot =a[left] then after completion a[j]= pivot, then return. Here j is the position where
// pivot partition the list into two partitions. Note that a[right]= .
pivot: a[left];
i:= left; j:=right;
repeat
{
repeat
i: =i+1;
until (a[i] ≥ pivot);
repeat
j: =j-1;
until (a[j] < pivot);
if( i<j) then
Swap (a, i, j);
}until (i ≥ j);
a[left]: = a[j];
a[j]: = pivot;
return j;
}
Algorithm Swap (a, i, j)
{
//Example a[i] with a[j]
temp:= a[i];
a[i]: = a[j];
a[j]:= temp;
}
Blog: anilkumarprathipati.wordpress.com 7
UNIT-2 DIVIDE AND CONQUER
Advantages of Quick-sort: Quick-sort is the fastest sorting method among all the sorting
methods. But it is somewhat complex and little difficult to implement than other sorting
methods.
Efficiency of Quick-sort: The efficiency of Quick-sort depends upon the selection of pivot
element.
Best Case: In best case, consider the following two assumptions-
1. The pivot, which we choose, will always be swapped into the exactly the middle of the
list. And also consider pivot will have an equal number of elements both to its left and
right.
2. The number of elements in the list is a power of 2 i.e. n= 2y.
Blog: anilkumarprathipati.wordpress.com 8
UNIT-2 DIVIDE AND CONQUER
Worst Case: In worst case, assume that the pivot partition the list into two parts, so that one of
the partition has no elements while the other has all the other elements.
Blog: anilkumarprathipati.wordpress.com 9
UNIT-2 DIVIDE AND CONQUER
The left child of each node represents a sub-problem size 1/4 as large, and the right child
represents a sub-problem size 3/4 as large.
There are log4/3 n levels, and so the total partitioning time is O(nlog4/3 n). Now, there's a
mathematical fact that
logan = logbn / logba
for all positive numbers a, b, and n. Letting a=4/3 and b=2, we get that
log4/3 n=log n / log(4/3)
Quick Sort
Merge Sort:
Merge sort is based on divide-and-conquer technique. Merge sort method is a two phase
process-
1. Dividing
2. Merging
Dividing Phase: During the dividing phase, each time the given list of elements is divided into
two parts. This division process continues until the list is small enough to divide.
Merging Phase: Merging is the process of combining two sorted lists, so that, the resultant list is
also the sorted one. Suppose A is a sorted list with n element and B is a sorted list with n2
elements. The operation that combines the elements of A and B into a single sorted list C with
n=n1 + n2, elements is called merging.
Algorithm-(Divide algorithm)
Algorithm Divide (a, low, high)
{
// a is an array, low is the starting index and high is the end index of a
Blog: anilkumarprathipati.wordpress.com 10
UNIT-2 DIVIDE AND CONQUER
The merging algorithm is as follows-
While (j high) do
{
B[k]=a[j];
K: = k+1;
j: =j + 1;
}
//copy elements of b to a
For i: = l to n do
{
A[i]: =b[i];
}
}
Blog: anilkumarprathipati.wordpress.com 11
UNIT-2 DIVIDE AND CONQUER
Ex: Let the list is: - 500, 345, 13, 256, 98, 1, 12, 3, 34, 45, 78, 92.
500 345 13 256 98 1 12 3 34 45 78 92
T(n) = { a
2T(n/2) + Cn
if n=1, a is a constant
if n>1, C is constant
Blog: anilkumarprathipati.wordpress.com 12
UNIT-2 DIVIDE AND CONQUER
Replace n by n/2 in equation, 1 ,we get
T (n/2) = 2T(n/4) + Cn 2
2
Thus, T(n) = 2 2T (n/4) + Cn + Cn
2
= 4T (n/4) + 2Cn
= 4T 2 T (n/8) + Cn + 2Cn
4
...
...
...
= 2 k T(1) + KCn ... k = log2 n
= a n + Cn log n
.
. . T (n) = O( n log n)
Blog: anilkumarprathipati.wordpress.com 13