c - binary search pointers dynamic memory allocation recursion -


i'm doing implementation of binary search pointers , dynamic memory allocation. first sort list of elements , perform search. sorting part works search isn't working properly. know algorithm correct have no idea how go syntax.could me out?? ps first question on stack overflow, btw. :)

#include <stdio.h> #include <stdlib.h>  int binary_search(int x,int start,int end, int *array) {    int q;    q=(start+end)/2;    if(x==*(array+q))    {       return q;    }    else if (x>*(array+q))    {       binary_search(x,q+1,end,array);    }     else if (x<*(array+q))    {       binary_search(x,start,q-1,array);    }     else if(start>=end)       return -1; }  int main() {    int n,*a,tmp,i,j,search,pos;    printf("enter n");    scanf("%d",&n);    a=malloc(sizeof(int)*n);     for(i=0;i<n;i++)    {       scanf("%d",&tmp);       *(a+i)=tmp;    }     for(i=0;i<n-1;i++)    {       for(j=0;j<n-1;j++)       {          if(*(a+j)>*(a+j+1))          {             tmp=*(a+j);             *(a+j)=*(a+j+1);             *(a+j+1)=tmp;          }       }    }     printf("sorted elements are\n");    for(i=0;i<n;i++)    {       printf(" %d",*(a+i));    }     printf("enter element search\n");    scanf("%d",&search);    pos=binary_search(search,0,n-1,&a);    printf("\nthe element located @ position %d",pos);     return 0; } 

in addition above answers -

int binary_search(int x,int start,int end, int *array)                                                 ^integer pointer 

but in main passing int ** type-

pos=binary_search(search,0,n-1,&a);                                ^a integer pointer 

compiler issue warning. pass a in this. , forgot free a have allocated memory using malloc.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -