Professional Documents
Culture Documents
DS Experiment No 09
DS Experiment No 09
INPUT SPECIFICATION:
User has to entered his choice of operation they want to do that may be insert, delete and search
functions in a binary search tree
OUTPUT SPECIFICATION:
As per output is printed. entered choices by user
Theory:
Searching is an operation or a technique that helps finds the place of a given
element or value in the list. Any search is said to be successful or unsuccessful
depending upon whether the element that is being searched is found or not.
Types of searching techniques
Linear Search or Sequential Search
Index Sequential Search
Binary Search
Interpolation Search
Hash Search
Binary search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item of
the collection. If a match occurs, then the index of item is returned. If the middle
item is greater than the item, then the item is searched in the sub-array to the left of
the middle item. Otherwise, the item is searched for in the sub-array to the right of
the middle item. This process continues on the sub-array as well until the size of
the subarray reduces to zero.
POGRAM CODE :
#include<stdio.h>
#include<conio.h>
int BinarySearch(int a[], int n, int key) {
int start = 0, last = n-1, mid;
while (start <= last) {
mid=(start + last)/2;
if(key == a[mid])
return mid;
else if (key < a[mid])
last = mid-1;
else
start = mid + 1;
}
return -1;
}
int main(){
int arry[20],i,n;
int flag, key;
//clrscr();
printf("\nEnter how many numbers you want to insert: ");
scanf("%d",& n);
printf("\nEnter the number\n");
for(i=0;i<n;i++)
scanf("%d",&arry[i]);
printf("\nEnter number to be serarch in given list:-");
scanf("%d",&key);
flag =BinarySearch(arry,n,key) ;
if(flag == -1)
printf("\nelement %d not found", key);
else
printf("\nelement %d found at position %d", flag);
getch();
return 0;
}
Conclusion:
Binary search is a fast search algorithm with run-time complexity of Ο(log n) when compared to
linear search whose worst case run-time complexity is O(n).