Algo Assignment-P2 Binary SRCH
Algo Assignment-P2 Binary SRCH
GROUP-A: Searching
Assign- 1) Binary Search Method (The program should report the number of pass
& comparisons)
PROCEDURE
Binary search is the search technique that works efficiently on sorted lists.
Hence, to search an element into some list using the binary search technique,
we must ensure that the list is sorted.
Binary search follows the divide and conquer 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. Otherwise, we search into either of the halves depending upon the
result produced through the match.
PROGRAM
int com=0;
int BinarySearch(int a[], int beg, int end, int item)
{
int mid;
if(end >= beg)
{
com++;
mid = (beg + end)/2;
if(a[mid] == item)
void main()
{
clrscr();
FILE *in;
int n, i, item, a[100], location=-1;
cout<<"Enter Sample Size (No of Elements):";
cin>>n;
printf("Enter the item which you want to search: ");
cin>>item;
in=fopen("Algo\\AInput.txt", "r");
i=0;
while(!feof(in))
{
fscanf(in, "%d", &a[i++]);
}
fclose(in);
getch();
}
if (arr[m] == x)
return m+1;
else
r = m - 1;
}
void main(void)
{
clrscr();
FILE *in;
int n, i, item, a[100];
printf("Enter Sample Size (No of Elements):");
scanf("%d", &n);
printf("Enter the item which you want to search: ");
scanf("%d", &item);
in=fopen("AInput.txt", "r");
i=0;
while(!feof(in))
fscanf(in, "%d", &a[i++]);
fclose(in);
OUTPUT
DISCUSSION
o Best Case Complexity - In Binary search, best case occurs when the
element to search is found in first comparison, i.e., when the first middle
element itself is the element to be searched. The best-case time
complexity of Binary search is O(1).
o Average Case Complexity - The average case time complexity of Binary
search is O(logn).
o Worst Case Complexity - In Binary search, the worst case occurs, when
we have to keep reducing the search space till it has only one element.
Literature Review