Enter 5 numbers
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
Searched Element Index:
No. of instructions count:
int binarySearch(INPUT Array arr, Index Start s, Index End e, Searching Item x)
e:Last Index of an array arr
s:Start Index of an array arr
arr: Input array arr
x: Item to search in Array arr
mid :external Variable
if (e >= s) {
int mid = s + (e- l) / 2;
// If the element is present at the middle of an array arr
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, s, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, e, x);
}
return -1;
}
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>
int count=0;
void main()
{
void getdata(int[10],int,int);
void putdata(int[10],int);
int binarysearch(int[],int,int,int);
int i,a[100],n;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
count++;
getdata(a,n,x);
count++;
count++;
printf("\nItem is at %d\n",binarySearch( arr, s, e, x));
count++;
printf("\n for n = %d value of count is %d",n,count);
getch();
}
void getdata(int x[10],int n,int x)
{
int k;
printf("Enter Item to Search \n");
scanf("%d",&x);
printf("Enter the value in array\n");
for(k=0; k < n; k++)
{
scanf( "%d",&x[k] );
}
}
void putdata(int x[10], int n)
{
int k;
for(k=0; k < n; k++)
{
printf("%d\t",x[k]);
}
printf("\n");
}
int binarySearch(int arr[], int s, int e, int x)
{
if (e >= s) {
int mid = s + (e - l) / 2;
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, s, mid - 1, x);
// Else the element can only be present in right subarray
else
return binarySearch(arr, mid + 1, e, x);
}
//Element is not present in array
return -1;
}
Best case | |
Average case | |
Worst case |