DAA - DAC - Basic - BinarySearch
DAA - DAC - Basic - BinarySearch
● 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.
● 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
● 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.