The code doesn't print the position of elements at the far left or right positions. I've looked at it for hours please help me find out whats wrong.
type here
#include<stdio.h>
#define max 50
void binarysearch(int l,int r,int key,int num[])
{
int mid=(l r)/2;
if(num[mid]==key)
{
printf("Element found at location %d",mid 1);return;
}
else if(num[mid]<key)
{
return binarysearch(l,mid-1,key,num);
}
else
{
return binarysearch(mid 1,r,key,num);
}
printf("Element not found ");return;
}
int main()
{
int i,key,size,num[max];
printf("Enter size of array : ");scanf("%d",&size);
for(i=0;i<size;i )
{
printf("Enter the element : ");scanf("%d",&num[i]);fflush(stdin);
}
printf("Enter key to search : ");scanf("%d",&key);
binarysearch(0,size-1,key,num);
return 0;
}
I'm gonna go insane. Send help
CodePudding user response:
For starters you need to check within the function whether r
is not less than l
. Without this check the function can invoke undefined behavior.
These if statements are incorrect
else if(num[mid]<key)
{
return binarysearch(l,mid-1,key,num);
}
else
{
return binarysearch(mid 1,r,key,num);
}
For example if num[mid]
is less than key
then you need to call the function recursively like
return binarysearch(mid 1, r, key,num);
otherwise like
return binarysearch(1, mid-1,key,num);
Pay attention to that the function should not output any message. It should returns to the caller either the index of the found element or for example -1
if the searched value is not found in the array. It is the caller of the function that will decide whether to output a message.
It would be much better to declare the function at least like
int binarysearch( const int num[], int size, int key );
or like
int * binarysearch( const int num[], size_t size, int key );
For the last declaration the function returns a pointer to the found element or NULL.
Here is a demonstration program that shows the function implemengtation.
#include <stdio.h>
int * binarysearch( const int a[], size_t n, int key )
{
if (n == 0)
{
return NULL;
}
else if (a[n / 2] < key)
{
return ( int * )binarysearch( a n / 2 1, n - n / 2 - 1, key );
}
else if ( key < a[n / 2] )
{
return ( int * )binarysearch( a, n / 2, key );
}
else
{
return ( int * )a n / 2;
}
}
int main( void )
{
int a[] = { 1, 3, 5 };
const size_t N = sizeof( a ) / sizeof( *a );
for (int key = 0; key < a[N - 1] 2; key)
{
int *p = binarysearch( a, N, key );
if (p != NULL)
{
printf( "The value %d is found at position %td.\n", key, p - a );
}
else
{
printf( "The value %d is not found.\n", key );
}
}
}
The program output is
The value 0 is not found.
The value 1 is found at position 0.
The value 2 is not found.
The value 3 is found at position 1.
The value 4 is not found.
The value 5 is found at position 2.
The value 6 is not found.
Pay also attention to that this call in your program
fflush(stdin);
has undefined behavior.
CodePudding user response:
You've simply wrote the code in opposite places.
The right code would be:
#include<stdio.h>
#define max 50
void binarysearch(int l,int r,int key,int num[])
{
int mid=(l r)/2;
if(num[mid]==key)
{
printf("Element found at location %d",mid 1);return;
}
else if(num[mid]<key)
{
// Wrong Code
// you've done the opposite operation
//return binarysearch(l,mid-1,key,num);
// Correct Code
return binarysearch(mid 1,r,key,num);
}
else
{
// Wrong Code
// same problem as before.. you've done opposite operation
//return binarysearch(mid 1,r,key,num);
// Correct Code
return binarysearch(l,mid-1,key,num);
}
printf("Element not found ");return;
}
int main()
{
int i,key,size,num[max];
printf("Enter size of array : ");scanf("%d",&size);
for(i=0;i<size;i )
{
printf("Enter the element : ");scanf("%d",&num[i]);fflush(stdin);
}
printf("Enter key to search : ");scanf("%d",&key);
binarysearch(0,size-1,key,num);
return 0;
}
Hope this would help.
CodePudding user response:
There are two problems:
1- Your arguments about the recursive called functions are wrong.
2- The recursive call is infinite.
You should change your binarysearch
function to this:
void binarysearch(int l, int r, int key, int num[])
{
int mid=(l r)/2;
if(l > r)
{
printf("Element not found");
return;
}
if(num[mid] == key)
{
printf("Element found at location %d", mid 1);
return;
}
if(num[mid] < key)
{
return binarysearch(mid 1, r, key, num);
}
return binarysearch(l, mid-1, key, num);
}