0% found this document useful (0 votes)
46 views24 pages

DAA - DAC - Basic - BinarySearch

The document discusses the divide and conquer algorithm design technique. It explains that divide and conquer breaks problems down into subproblems, solves the subproblems recursively, and then combines the solutions. Examples provided are binary search, quicksort, and merge sort. The time complexity of binary search is analyzed to be O(log n) as each iteration cuts the search space in half. Advantages include efficiency while disadvantages include potential issues with recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views24 pages

DAA - DAC - Basic - BinarySearch

The document discusses the divide and conquer algorithm design technique. It explains that divide and conquer breaks problems down into subproblems, solves the subproblems recursively, and then combines the solutions. Examples provided are binary search, quicksort, and merge sort. The time complexity of binary search is analyzed to be O(log n) as each iteration cuts the search space in half. Advantages include efficiency while disadvantages include potential issues with recursion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Design and Analysis of Algorithms

Divide and Conquer


Binary Search
Divide and Conquer
● Divide and conquer approach breaks down a problem into multiple sub-
problems recursively until it cannot be divided further. These sub-
problems are solved first and the solutions are merged together to form
the final solution.

● Examples:
● Task Management
● Study and Learn
● Cleaning and Organizing:
Contd…
● The common procedure for the divide and conquer design technique is as
follows −
● Divide − We divide the original problem into multiple sub-problems until they
cannot be divided further.
● Conquer − Then these subproblems are solved separately with the help of
recursion
● Combine − Once solved, all the subproblems are merged/combined together
to form the final solution of the original problem.
Contd…
Contd…
DAC(P)
{
If (small(P))
{
S(P);
}
Else
{
Divide P into p1,p2,p3,p4,…pk
Apply DAC(p1), DAC(p2),DAC(p3),…DAC(pk)
Combine (DAC(p1), DAC(p2),DAC(p3),…DAC(pk))
}
}
Examples / Applications
● Binary Search
● Find max and min
● Quicksort
● Merge Sort
● Strassen's Matrix Multiplication
Advantages
● This algorithm is much faster than other algorithms.
● It efficiently uses cache memory without occupying much space because it
solves simple subproblems within the cache memory instead of accessing the
slower main memory.
● It is more proficient than that of its counterpart Brute Force technique.
● Since these algorithms inhibit parallelism, it does not involve any
modification and is handled by systems incorporating parallel processing.
● It divides the entire problem into subproblems thus it can be solved parallelly
ensuring multiprocessing
● It reduces the time complexity of the algorithm by breaking down the
problem into smaller sub-problems.
Disadvantages of DAC
● It involves recursion which is sometimes slow
● Efficiency depends on the implementation of logic
● It may crash the system if the recursion is performed rigorously.
● It can be difficult to determine the base case or stopping condition for
the recursive calls.
● It may require more memory than other algorithms because it involves
storing intermediate results.
Searching
● Searching is a process of finding a particular element among several
given elements.
● The search is successful if the required element is found.
● Otherwise, the search is unsuccessful.

● Two popular search methods are Linear Search and Binary Search.
Linear Search
● Linear search is also called a sequential search algorithm. It is the
simplest searching algorithm. In Linear search, we simply traverse the list
completely and match each element of the list with the item whose
location is to be found.
● If the match is found, then the location of the item is returned;
otherwise, the algorithm returns NULL.
Binary Search
● Binary Search is one of the fastest searching algorithms.
● Binary search follows the divide and conquers approach in which the list
is divided into two halves, and the item is compared with the middle
element of the list. If the match is found then, the location of the middle
element is returned.

● Binary Search Algorithm can be applied only on Sorted arrays.


● So, the elements must be arranged in-
● Either ascending order if the elements are numbers.
● Or dictionary order if the elements are strings.
Binary Search
● We basically ignore half of the elements just after one comparison.
● Case-01: If the element being searched is found to be the middle most
element, its index is returned.
● Case-02: If the element being searched is found to be greater than the
middle most element, then its search is further continued in the right sub
array of the middle most element.
● Case-03: If the element being searched is found to be smaller than the
middle most element, then its search is further continued in the left sub array
of the middle most element.

● This iteration keeps on repeating on the sub arrays until the desired element
is found or size of the sub array reduces to zero.
Binary Search
● Let the element to search is, K = 56

● mid = (beg + end)/2


● So, in the given array -
● beg = 0
● end = 8
● mid = (0 + 8)/2 = 4. So, 4 is mid of the array.
Binary Search - Iterative Method
int BinSearch(A, n, key) if(key<A[mid])
{ h=mid-1
l=1, h=n; else
while(l<=h) l=mid+1;
}
{
return 0;
mid=l+h/2;
}
if(key==A[mid])
return mid; O(log n)
Best Case O(1)
Binary Search - recursive Method
RBinSearch(l,h,key) if(key==A[mid])
{ return mid;
if(l==h)
if(key<A[mid])
{
if(A[l]==key) return RBinSearch(l,mid-1,key)
return l; else
else return RBinSearch(mid+1,h,key);
return 0; }
} }
else
{
mid = l+h/2; T(n) = T(n/2)+C
Contd…
T(n) = 1 if n=1 T(n) = T(n/2k) + KC
= T(n/2)+C if n>1 n/2k = 1
n = 2k
T(n) = T(n/2) + C k = log n
T(n/2) = T(n/4) + C
T(n/4) = T(n/8) + C
T(n) = T(1) + log n * C
Substitute.. = 1 + log n * C
T(n) = T(n/4) + C + C
= T(n/22) + 2C
T(n) = O(log n)
= T(n/23) + 3C
….. K times
Binary Search
● Case-01: If the element being searched is found to be the middle most element, its
index is returned.

● Case-02: If the element being searched is found to be greater than the middle most
element, then its search is further continued in the right sub array of the middle most
element.

● Case-03: If the element being searched is found to be smaller than the middle most
element, then its search is further continued in the left sub array of the middle most
element.

● This iteration keeps on repeating on the sub arrays until the desired element is found or
size of the sub array reduces to zero.
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 log2n iterations or recursive calls.
● Thus, we have, Time Complexity of Binary Search Algorithm is O(log2n). Here,
n is the number of elements in the sorted linear array.

● This time complexity of binary search remains unchanged irrespective of the


element position even if it is not present in the array.
Binary Search Algorithm Advantages
● 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.
Binary Search Algorithm Disadvantages
● It employs a recursive approach which requires more stack space.
● Programming binary search algorithm is error-prone and difficult.
● The interaction of binary search with memory hierarchy i.e. caching is
poor. (because of its random access nature)
From
Gurpreet Singh Chhabra
What does not qualifies as DAC
● Binary Search is a searching algorithm. In each step, the algorithm
compares the input element x with the value of the middle element in
the array. If the values match, return the index of the middle. Otherwise,
if x is less than the middle element, then the algorithm recurs for the left
side of the middle element, else recurs for the right side of the middle
element. Contrary to popular belief, this is not an example of Divide and
Conquer because there is only one sub-problem in each step (Divide and
conquer requires that there must be two or more sub-problems) and
hence this is a case of Decrease and Conquer.
Contd…
● For in-memory searching, if the interval to be searched is small,

● Linear search may exhibit better performance than binary search.


● This is because it exhibits a better locality of reference.
Binary Search
● Begin ● if (beg > end) then
● Set beg = 0
● Set end = n-1 ● Set loc = -1
● Set mid = (beg + end) / 2 ● else
● while ((beg <= end) and (a[mid] ≠ item))
do ● Set loc = mid
● if (item < a[mid]) then ● endif
● Set end = mid - 1
● else ● End
● Set beg = mid + 1
● endif
● Set mid = (beg + end) / 2
● endwhile

You might also like