Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 08-10-2008, 06:37 PM   #1 (permalink)
In The Zone
 
Join Date: Sep 2005
Posts: 227
Default Binary Search in C++ using old code, search doesnt register element in last position


hey,
Using old code(non-ANSI)
The search detects elements in all positions in the 1-d array, except the last one
pastebin- http://pastebin.com/f4db70a8b
Quote:
//program to run binary search
#include <iostream.h>
int main ()
{
int a[10],n,x,pos=-1,i,first,last,mid;
cout<<" Enter the number of elements into the array ";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"\n Enter element number "<<i+1<<" ";
cin>>a[i];
}
cout<<"Enter the number to be searched \t";
cin>>x;
first=0;
last=n-1;
while((first<=last)&&(pos==-1))
{
mid=(first+last)/2;
if(a[mid]==x)
pos=mid;
else
last=mid-1;
}
if(pos==-1)
cout<<" \t Element not found ";
else
cout<<" Element found at position "<<pos+1;
return 0;
}
Does anyone see my mistake? debugging is not my strong suit
__________________
Our deepest fear is not that we are inadequate Our deepest fear is that we are powerful beyond measure
karmanya is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 08-10-2008, 07:47 PM   #2 (permalink)
Back!
 
red_devil's Avatar
 
Join Date: Jun 2007
Location: Bangalore
Posts: 513
Default Re: Binary Search in C++ using old code, search doesnt register element in last posi

Quote:
Originally Posted by karmanya
#include <iostream.h>
int main ()
{
int a[10],n,x,pos=-1,i,first,last,mid;
cout<<" Enter the number of elements into the array ";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"\n Enter element number "<<i+1<<" ";
cin>>a[i];
}
cout<<"Enter the number to be searched \t";
cin>>x;
first=0;
last=n-1;
while((first<=last)&&(pos==-1))
{
mid=(first+last)/2;

if(a[mid]==x)
pos=mid;
else
last=mid-1;

}
if(pos==-1)
cout<<" \t Element not found ";
else
cout<<" Element found at position "<<pos+1;
return 0;
}
AFAIK, the logic is wrong mate...

Binary search should divide the array of elements into 2 and then find out which half the element is likely to be found..{assuming the array is sorted } and then narrow the search down to that particular half ...

the code you've posted doesn't do that..

it should be something like :



if ( element to be searched ) < the middle element,
then search the array from first element to an element before middle element
else
search the array from the element after the middle element to the last element


all this should be done within a conditional loop, ofcourse.

PS : it was soo tempting to write the code itself... but i guess you'd want to do it on your own ...

Last edited by red_devil; 08-10-2008 at 07:54 PM. Reason: Automerged Doublepost
red_devil is offline  
Old 08-10-2008, 10:33 PM   #3 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: Binary Search in C++ using old code, search doesnt register element in last posi

Here, I'll write a pseudocode, I prefer it as a function due to recursive calling, returns array index if found, -1 if not.

Code:
int binsearch (int a[], int start = 0, int end = sizeof(a)/sizeof(int), int searchelement)
{
   mid = (start + end) / 2;
   if (a[mid] == searchelement) //bingo, got it
      return mid;
   else if ((a[mid] > searchelement) && (mid != start)) //to prevent infinite loops
      return binsearch (a[], start, mid, searchelement);
   else if ((a[mid] < searchelement) && (mid != start)) //to prevent infinite loops
      return binsearch (a[], mid, end, searchelement);
   else //to actually exit from the function
      return -1;
}
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 09-10-2008, 10:55 AM   #4 (permalink)
In The Zone
 
Join Date: Sep 2005
Posts: 227
Default Re: Binary Search in C++ using old code, search doesnt register element in last posi

Thanks guys, Thanks to QwertyM who helped me fix it.
last=mid instead of mid-1 fixed it.
__________________
Our deepest fear is not that we are inadequate Our deepest fear is that we are powerful beyond measure
karmanya is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a firefox search engine plugin in search box blademast3r Internet & WWW 1 13-06-2008 06:25 PM
Search for Passwords with Google Search Engine arunsmartkid Technology News 2 13-05-2008 01:18 AM
Binary Search NagpurDaMunda QnA (read only) 8 21-04-2007 01:04 PM
Inserting AdSense for search code whoopy_whale QnA (read only) 1 28-07-2005 06:32 PM
Search and find your Google position without certain filters expertno.1 Internet & WWW 1 18-05-2005 06:00 PM

 
Latest Threads
- by Sujeet
- by gforz
- by soumya

Advertisement




All times are GMT +5.5. The time now is 03:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2