Jump to content

Binary Search How to have just number argument

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
18 replies to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I know this code for binary search

int search(int a[], int key, int low, int high) {

    if (high < low) {

        return NOT_FOUND; 

    }

    int mid = (low + high) / 2;

    if (key>a[mid]) {

        return searchName(a, key,low, mid-1);

    } else if (key<a[mid]) {

        return searchName(a, key,mid+1, high);

    } else {

        return EXIT_SUCCESS; 

    }

    return ERROR_SEARCH;

}

Now I want to make recursive one which just need
int search(char *dict, char *name,int length,int compChars)
and length is number element in array.
please help me.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The code you have is already recursive. What do the variables in your new function signature represent?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Thanks for your reply.
I need another recursive function which call like this
bs(int array[],int key,int len);
it should be recursive and key is number that I try to find it and len is number elements in array.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can pass &(array[min]) as the first parameter, and max-min for len.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
int bs(int array[],int n,int key)
{
	int mid,low=0,high=n-1;
	mid=n/2;
	if(array[mid] == key)
	{
		return(mid);
	}
	else if(array[mid]>key)
	{
		bs(array,high-low,key);
	}
	else if(array[mid]<key)
	{
		bs(array,high-low,key);
	}
	else if(high<low)
	{
         return(-1);
    }
    else{
         return(-1);
    }
}
always give me right answer
I don't know what change I need!!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Try this:
int bs(int array[],int n,int key)
{
	int mid,low=0,high=n-1;
	mid=n/2;
	if(array[mid] == key)
	{
		return(mid);
	}
	else if(array[mid]>key)
	{
		bs(&(array[mid]),high-low,key);
	}
	else if(array[mid]<key)
	{
		bs(&(array[low]),high-low,key);
	}
	else if(high<low)
	{
         return(-1);
    }
    else{
         return(-1);
    }
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Yet It doesn't work. Should I change anything when I call function?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have you run it through a debugger? What is it doing?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
This is my program
I save it as php file because just I can upload it as php file or txt. (.c or .cPP not allowed)
You can test it.

Attached Files

  • Attached File  re.php   1.26K   25 downloads


#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm reposting the code here for ease of use:
#include <stdio.h>
#include <conio.h>
void bsort(int array[], int n)
{
  int i, j, temp;
  for (i = (n - 1); i >= 0; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (array[j-1] > array[j])
      {
        temp = array[j-1];
        array[j-1] = array[j];
        array[j] = temp;
      }
    }
  }
}
void giveinput(int array[],int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("Enter number %d:",i+1);
		scanf("%d",&array[i]);
	}
}
void giveoutput(int array[],int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("%d\t",array[i]);
	}
}
int bs(int array[],int n,int key)
{
	int mid,low=0,high=n-1;
	mid=n/2;
	if(array[mid] == key)
	{
		return(mid);
	}
	else if(array[mid]>key)
	{
		bs(&(array[high-low]),high-low,key);
	}
	else if(array[mid]<key)
	{
		bs(&(array[high-low]),high-low,key);
	}
	else if(high<low)
	{
         return(-1);
    }
    else{
         return(-1);
    }
}
int main()
{
    int array[5],n=5,key;
    giveinput(array,n);
    bsort(array,n);
    printf("Enter a number that I should search for it:",key);
    scanf("%d",&key);
    
    if(bs(array,n,key)>0)
    {
                          printf("We have it:%d\n",bs(array,n,key));
    }
    giveoutput(array,n);
    getche();
    return(0);
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
what does &(array) do?

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Return the address of the element. Whenever you are passing arrays into functions, you are really passing a pointer to an element (usually the first) in the array.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog