Algorithms
Algorithms
Algorithms
Ans: - The binary search implemented only sorted list of elements. The process starts with
finding the element at the middle of the list and comparing the key value with the element in
the middle of the list, if the value of the key is less than the element at the middle then the
search process the key to the list up to the middle element and if the value of the key is
greater than element at the middle then search the key from middle element to the end of the
list.
The complexity of binary search is o (log n).
Algorithm:-
Binary search(A[0,1,2,3…..n-1],key)
low <-0
high<-1
while(low<=high)
do
{
mid<-(low high)/2
if(key=A[mid])
then
return mid
else if(key<A[mid])
then
high<-m-1
else
low<-m+1
}
Example:-
Perform binary search on below list for finding value 13.
6 13 14 25 33 43 51 53 64 72 84 93 95 98
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Pass1:-
6 13 14 25 33 43 51 53 64 72 84 93 95 98
0 1 2 3 4 5 6 7 8 9 10 11 12 13
1
low mid high
6 13 14 25 33 43
0 1 2 3 4 5
Pass2:-
6 13 14
0 1 2
Pass3:-
2
2) Explain Brute Force, Greedy, Divide and Conquer and Dynamic programming
techniques. And which algorithms are under in these techniques?
Ans:-
Brute Force: - brute force is a straightforward approach to solving a problem, usually
directly based on the problem statement and definitions of the concept involved.
It’s used for many elementary but algorithmic tasks such as computing the sum of n numbers,
finding the largest element in a list and so on.
Greedy: the greedy is most straightforward design technique which can be applied to a wide
variety of problems. This algorithm works in steps. It is easy to implement and quite efficient
in most of the cases.
Greedy algorithms:-
3
Binary search
Quick sort
Merge sort
Dynamic programming: -
Dynamic programming is an algorithm technique for solving an optimization problem by
breaking it down into simpler sub problems and utilizing the fact that the optimal solution to
the overall problems depends upon the optimal solution to its sub problems.
3) Short Note
a) Algorithm specification
Ans:-
An algorithm is defined as finite set of instructions that, if followed, performs a particular
task. All algorithms must satisfy the following criteria
Input: - An algorithm has zero, taken or collected from specified set of objects.
Output: - An algorithm has one or more outputs having a specific relation to the inputs.
Definiteness. Each step must be clearly defined. Each instruction must be clear and
unambiguous.
Finiteness: - the algorithm must always finish or terminate after a finite number of steps.
Effectiveness. All operations to be accomplished must be sufficiently basic that they can be
done exactly and in finite length.
b) Induction proof
Ans: - A proof by induction is just like an ordinary proof in which every step must be
justified.
Proof is a sequence of deductive steps
4
Basis step: - The algorithm is correct for base case or two by inspection.
Inductive hypothesis (n=k):-Assume that the algorithm works correctly for the first K
cases.
Inductive step (n=k+1):- Given the hypothesis above, show that K+1 case will be
calculated correctly.
So suppose u r given array of size n which contains elements of varying from 0 to n-1. You
can use the index of elements to find repeated elements.
Algorithms:-
2) 1st elements is 2 so check element in array with index 2 is >=0 if yes then change
the element in array index 2 to -1*arr[abs(arr[i])]
5
Example:-
arr[]=2,3,4,3,2,6,4
e) Recursion
Ans:-
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding is called recursive function. Using recursive algorithm, certain problems can
be solved quite easily.