473,419 Members | 2,686 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Binary Search

Could Someone tell me Where can I get The result though the Binary
Search
eg.
max , min , mid ,want;
While ( max > min )
{
mid = (min + max )/2 ;
if ( mid > want ) max = mid + 1;
else min = mid -1 ;
Line1 :
}
Line2 :
Where can I get the correct result ? Line1 or Line2 ?

Nov 15 '05 #1
2 1863
>>Could Someone tell me Where can I get The result though the Binary
Search
eg.
max , min , mid ,want;
While ( max > min )
{
mid = (min + max )/2 ;
if ( mid > want ) max = mid + 1;
else min = mid -1 ;
Line1 :
} Line2 :
Where can I get the correct result ? Line1 or Line2 ?


The best way to answer these questions is to take a sample ( an array
and a key in this case ), and then run through your algorithm/code for
both positive and negative cases.

Nov 15 '05 #2

"Sandeep" <sa************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Could Someone tell me Where can I get The result though the Binary
Search
eg.
max , min , mid ,want;
While ( max > min )
{
mid = (min + max )/2 ;
if ( mid > want ) max = mid + 1;
else min = mid -1 ;
Line1 :
}Line2 :
Where can I get the correct result ? Line1 or Line2 ?


The best way to answer these questions is to take a sample ( an array
and a key in this case ), and then run through your algorithm/code for
both positive and negative cases.


I agree the best way is to run through the program and see how it behaves,
but I also think one needs to understand the principle of the algorithm one
is trying to implement.
There are actually two "correct" results: "match" and "no match".
"Match" is where "DATA(mid) = want" and usually results in exiting from the
while loop before the default termination. ("max", "mid" and "min" are
usually pointers or indexes.)
"No match" is where the while loop terminates when "max" <= "min".
This sample routine doesn't even check for a match and if one looks at the
way "min" and "max" are updated, it may result in an endless loop as it
narrows the search to a few data entries. If it were working correctly,
"mid" would be a place to insert "want", if no match was found or it points
to where "want" is, if a match is found.

HTH,

-NM

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ravi | last post by:
I recently had to write some code which required me to use a binary search tree (BST) due to running time concerns. I found that there is no BST class in the Standard C++ library. I have been told...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
8
by: Gordon Knote | last post by:
Hi can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++). The search should be as fast as possible and it would...
1
by: Andrew | last post by:
Hi, im trying to create a small function which can create a binary tree from the entries in a text file and after that we can perform all the usual operations like search, insert and delete etc....
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is...
0
by: Vince Filby | last post by:
Hi, We are working with distributing Lucene.net. We have Master Index Server which takes responsibility of distributing the index searching to multiple Index Servers by calling the remote...
2
by: jhansi | last post by:
create a binary file and insert the records and query on those inserted record.. queries are: query based on search by name,search by age and search by designation. the problem is i have...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.