473,387 Members | 1,766 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,387 software developers and data experts.

A binary search with even amount of numbers

In a binary search, with an even amount of numbers, will the algorithm choose the higher or lower middle number?

So if the array is:

15 18 18 21 26 31 36 38

Then will it start by looking at 21 or 26?
Nov 6 '11 #1
1 57386
Stewart Ross
2,545 Expert Mod 2GB
For an even-number of values N within a range being searched the mid-point is at value N/2, but the two 'halves' are of slightly different sizes. For N=8 the first value tested will be the fourth value, in your case 21. This leaves three values in the lower 'half', and four values in the upper half.

For an odd number of values N/2 would lie between two values. As a choice has to be made of what value to compare it is possible to either take the integer part of N/2 and ignore the decimal part (by using integer division), or round up the mid-point to the next-nearest value. With 9 values, for example, integer division would give a mid-point at value 4, with 3 values in the lower 'half' and five values in the 'upper'. Rounding up the mid-point leaves the lower and upper halves with the same number of values (four each in the case of 9 values overall).

For odd numbers, the small difference in choosing Int(N/2) as the mid point as opposed to Int(N/2)+1, say, makes very little difference in practice to overall performance as the chance of the value being searched for being in the current 'half' is more or less the same in both cases.

For further information, see for example this Wikipedia article

-Stewart
Nov 6 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

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,...
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;
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
2
by: pankajit09 | last post by:
Hello, I have a textbox in which I have to enter a string of numbers. The numbers are to be converted in to a Binary Search Tree and then I have to determine whether the BST is a --> 1....
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
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...
4
praclarush
by: praclarush | last post by:
Hey everyone, I’m new at Java and I’m having a bit of trouble. I have an assignment in which I have an Array of objects. The user is to input a name, which then needs to be found in the Array so that...
1
by: yogi_bear_79 | last post by:
I am enrolled in distance learning class, this amounts to self taught. I have a book and that is about it. below is my assingment. The book doesn't prove useful for examples, and I haven't had...
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.