|
-
May 19th, 2006, 10:29 PM
#1
I have a project due and I really need some help now
Sorry to double post. I really some help with converting my binary search that works with ascending list to work with descending list.
here the code that works with ascending list
// See if the key is in the upper or lower half of the list and keep track of compares
// Until the middle equals the key
while((list[middle]!=key)&&(lower<=upper))
{compares=compares+1;
if (list[middle]>key)
upper=middle-1;
else
lower=middle+1;
middle=(lower+upper)/2;
}// End while
// The key is found tell the user its position
if (lower<=upper)
{tmp=middle+1;
cout<<"The key was found in position "<<middle+1<<"."<<endl;
cout<<"It took "<<compares<<" compares to find the value. "<<endl;
}// End if
// If the key isn't found return the dummy value
if (tmp==0)
return -1;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|