thanx a lot Now it is working for all cases......
On Thu, Jun 9, 2011 at 11:28 AM, varun pahwa <[email protected]>wrote:
> may be now it works for all. please correct me if i am wrong.you haven't
> put termination in binary search.
>
>
> #include<iostream>
> using namespace std;
> void merge(int A[],int p,int q,int r)
> {
> int n1=q-p+1;int n2=r-q;
> int L[n1];
> int R[n2];
> for(int i=0;i<n1;i++)
> L[i]=A[p+i];
> for(int j=0;j<n2;j++)
> R[j]=A[q+j+1];
> int i=0,j=0,k;
>
> for(k=p;k<r;k++)
> {
> if((L[i]<=R[j])&&i<n1)
> {A[k]=L[i];i++;}
> else
> {A[k]=R[j];j++;}
> }
>
> for(;i<n1;i++)
> {A[k]=L[i];k++;}
>
> for(;j<n2;j++)
> {A[k]=R[j];k++;}
>
> }
>
> //Merge Sort
> void merge_sort(int A[],int p,int r)
> {
> int q;
> if(p<r)
> {
> q=(p+r)/2;
> merge_sort(A,p,q);
> merge_sort(A,q+1,r);
> merge(A,p,q,r);
> }
> }
>
> //binary search
> int binary_search(int A[],int left,int right,int val)
> {
> int result;
> int mid=(left+right)/2;
> if(left < right)
>
> {
> if((right-left)==1&&val!=A[mid])
> result=-1;
> if(val<A[mid])
> result=binary_search(A,left,mid-1,val);
> else if(val>A[mid])
> result=binary_search(A,mid+1,right,val);
> else
> result= mid;
> return result;
> }
> return -1;
> }
>
>
> //main function
> int main()
> {
> int A[]={3,2,4,6,1,2,4,5,7};
> merge_sort(A,0,8);
>
> for(int i=0;i<9;i++)
> cout<<A[i]<<" ";
> cout<<endl;
> int x;
> for(int pqr = 0; pqr < 20; pqr++)
>
> {
> cout<<"Enter value of x(=sum of two items in array)"<<endl;
> cin>>x;
> int pos;int flag=0;int i;
> for( i=0;i<10;i++)
> {
> if((x-A[i])>0&&(pos=binary_search(A,i+1,9,x-A[i]))>=0)
>
> {flag=1;break;}
> }
> if(flag)
> {
> cout<<"There are two whose sum is equal to given number"<<endl;
> cout<<"Those numbers are "<<A[i]<<" and "<<A[pos]<<endl;
> }
> else
> cout<<"There are no such two numbers"<<endl;
> }
> return 0;
> }
>
>
>>> --
>>> **With Regards
>>> Deoki Nandan Vishwakarma
>>> IITR MCA
>>> *
>>> *
>>>
>>>
>>
>>
>> --
>> **With Regards
>> Deoki Nandan Vishwakarma
>> IITR MCA
>> *
>> *
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> Varun Pahwa
> B.Tech (IT)
> 7th Sem.
> Indian Institute of Information Technology Allahabad.
> Ph : 09793899112 ,08011820777
> Official Email :: [email protected]
> Another Email :: [email protected]
>
> People who fail to plan are those who plan to fail.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
**With Regards
Deoki Nandan Vishwakarma
IITR MCA
*
*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.