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

binary search questions help

Hi! Could someone please help me with the following binary search questions? (explain please!) Thanks.

A binary serach will be performed on the following list:

4, 7, 9, 11, 20, 24, 30, 41

1. To find the key value 27, the search interval after the first pass through the loop will be?

2. How many iterations will be required to determine that 27 is not on the list?

3. For which set of data will a sequential search perform more efficently than a binary search, if the value being searched for is 5?

a. 4, 5, 6, 8, 9, 11
b. 1, 2, 4, 5, 7, 10
c. 2, 4, 6, 8, 10, 12
d. 1, 2, 3, 4, 5, 6
e. 0, 1, 2, 3, 4, 5
Nov 17 '08 #1
3 1684
Ganon11
3,652 Expert 2GB
Do you know the concept of a binary search?

Have you possibly seen a Java implementation of the binary search (maybe in a textbook you're using)?
Nov 18 '08 #2
Do you know the concept of a binary search?

Have you possibly seen a Java implementation of the binary search (maybe in a textbook you're using)?
I just begun doing it in class today but I don't understand it at all.....

yes I've seen an implementation but it doesn't make much sense to me.
Nov 18 '08 #3
Nepomuk
3,112 Expert 2GB
OK, let's see.

You have some values given. If you want to put them in a binary tree, you'll have to decide, how to sort them.

Now, what does a binary tree look like? Here are two little pictures:
Expand|Select|Wrap|Line Numbers
  1.         O
  2.       /   \
  3.     O       O
  4.    / \     / \
  5.   O   O   L   O
  6.  / \ / \     / \
  7. L  L L  L    L  L
  8.  
Expand|Select|Wrap|Line Numbers
  1.           O
  2.         /   \
  3.       O       L
  4.      / \     
  5.     O   L
  6.    / \
  7.   O   L
  8.  / \
  9. L   L
  10.  
Now, every O is an inner knot and every L is a leaf. As you see, not all leaves must have the same distance from the root knot.

What is the big difference between those two trees I drew? (Apart from the amount of knots that is.) Right, the first one is more balanced. Balanced trees are very good for searching while the second possibility is easy to remove knots from.

OK, now these are just a general binary trees. There are different sorts however. First of all, you can differ between leaf based saving (values are saved only in leaves) and knot based saving (values are saved in inner knots only). Both types of binary trees have their advantages.

If you want to use such a tree efficiently, you organise it in such a way that for every subtree with the root T:
Expand|Select|Wrap|Line Numbers
  1.   T
  2.  / \
  3. L   R
  4.  
and L and R being subtrees, the following are valid:
  • All values in L are smaller than the value in T
  • All values in R are greater than the value in T
So, how would you sort those numbers in such a tree?

Oh, and let's assume you save your values in the knots. When do you know, that a value isn't in the tree?

Greetings,
Nepomuk
Nov 18 '08 #4

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

Similar topics

7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
11
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
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,...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
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...
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...
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: 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
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...
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
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
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...

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.