Thursday, 27 July 2017

DAA PRACTICAL MANUAL: PRACTICAL 2

                                                                                                      Date:27-07-2017
Experiment No: 02

1. Experiment vision: Write a program to study and implement Binary search.
2. OBJECTIVE: Program to implement an array of object in C and to find the specific 
                              element using divide method.  
3. THEORY:  In computer science, a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value. In each step, the algorithm compares the search key value with the key value of the middle element of the array. If the keys match, then a matching element has been found and its index, or position, is returned. Otherwise, if the search key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the search key is greater, on the sub-array to the right. If the remaining array to be searched is empty, then the key cannot be found in the array and a special "not found" indication is returned. A binary search halves the number of items to check with each iteration, so locating an item (or determining its absence) takes logarithmic time.
4.  PROGRAM:
#include<stdio.h>
void main()
{
int c,first,last,middle,n,search,array[10];
printf("enter no of element");
scanf("%d",&n);
printf("enter %d integer\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("enter a no to be search");
scanf("%d",&search);
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(array[middle] < search)
first=middle+1;
else if(array[middle]==search)
{
printf("%d found at location %d\n",search,middle+1);
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
printf("not found");
}

5. OUTPUT:
------------------------------------------
------------------------------------------

6. conclusion:
Thus C programs for binary search implement and executed successfully.
7.  REMARK:
Array of element is the concepts are used for printing the values of number element and to find the element using implementing binary search method.
In that we work on divide and conquer method.  
8. Discussion Questions:
1) what is binary search?
_________________________________________________________________________________________________________________________________________________________
2) How is binary search working explain with example? (diagrammatically)
___________________________________________________________________________________________________________________________________________________________
3) What is the complexity of binary search method?
__________________________________________________________________________________________________________________________________________________________________

                                    

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.