473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How many elements would have to be looked to locate these elements?

74 New Member
How many elements would have to be looked (binary search) at in order to find an integer 5000 at element 499 and an integer 7282 at element 686 assuming it's binary search algorithm and total number of elements in the array are 1000?

I do have a theory though (see my theory below:)

1000/2= the first element is 500 element

500/2 the next element is 250 element.
Sep 8 '17 #1

✓ answered by Frinavale

You seem to know what you are doing.

The theory behind a binary search is that you are looking within an ascending sorted list and you search for it by cutting the list in half to check to see if it is the value you are looking for...if it's not you check to see if the if the value you are looking for is higher or lower than what you landed at and cut that half of the list in half and do it again.

A basic binary search algorithm is pretty much as follows:
  1. Set LeftPosition to 0 and RightPosition to the number of items int the array − 1.
  2. If LeftPosition > RightPosition, the search terminates as unsuccessful because your list is empty or you have searched through the whole array.
  3. Set MidPoint (the position of the middle element) to the floor (the largest previous integer) of (LeftPostition + RightPostion) / 2.
    • If ValueAtTheMidPointPostitionInTheArray < WhatYouAreLookingFor, set LeftPostion to MidPoint + 1 and go to step 2.
    • If ValueAtTheMidPointPostitionInTheArray> WhatYouAreLookingFor, set RightPosition to MidPoint − 1 and go to step 2.
  4. Now ValueAtTheMidPointPostitionInTheArray= WhatYouAreLookingFor, the search is done; return MidPoint.

The Big-O notation for the worst case (when you don't find anything) is O(log n) because you are constantly dividing the list in half and so the operations required to complete the search decrease with each iteration.

You should step through the algorithm and draw out your trees to figure out how many iterations it takes to get to each element-position that you are looking for.

Your first search tree(search for index 499) looks something like:
Expand|Select|Wrap|Line Numbers
  1.             (1000)
  2.             /    \            
  3.        (0-500)   (501-999)
  4.       /     \
  5. (0-250)  (251-500)
  6.           /      \
  7.   (251-375)      (376-500)
  8.                  /      \
  9.           (376-438)    (439-500)
  10.                         /     \
  11.                  (438-469)   (470-500)
  12.                              /      \
  13.                        (469-485)   (486-500)
  14.                                    /    \
  15.                             (485-493)  (494-500)
  16.                                         /    \
  17.                                  (493-497) (498-500)
  18.                                             /   \
  19.                                          (499)  (500)
  20.                                           |
  21.                                        (found)
  22.  

2 3866
dseals22
74 New Member
Do you think you can help me @weaknessforcats?
Sep 8 '17 #2
Frinavale
9,735 Recognized Expert Moderator Expert
You seem to know what you are doing.

The theory behind a binary search is that you are looking within an ascending sorted list and you search for it by cutting the list in half to check to see if it is the value you are looking for...if it's not you check to see if the if the value you are looking for is higher or lower than what you landed at and cut that half of the list in half and do it again.

A basic binary search algorithm is pretty much as follows:
  1. Set LeftPosition to 0 and RightPosition to the number of items int the array − 1.
  2. If LeftPosition > RightPosition, the search terminates as unsuccessful because your list is empty or you have searched through the whole array.
  3. Set MidPoint (the position of the middle element) to the floor (the largest previous integer) of (LeftPostition + RightPostion) / 2.
    • If ValueAtTheMidPointPostitionInTheArray < WhatYouAreLookingFor, set LeftPostion to MidPoint + 1 and go to step 2.
    • If ValueAtTheMidPointPostitionInTheArray> WhatYouAreLookingFor, set RightPosition to MidPoint − 1 and go to step 2.
  4. Now ValueAtTheMidPointPostitionInTheArray= WhatYouAreLookingFor, the search is done; return MidPoint.

The Big-O notation for the worst case (when you don't find anything) is O(log n) because you are constantly dividing the list in half and so the operations required to complete the search decrease with each iteration.

You should step through the algorithm and draw out your trees to figure out how many iterations it takes to get to each element-position that you are looking for.

Your first search tree(search for index 499) looks something like:
Expand|Select|Wrap|Line Numbers
  1.             (1000)
  2.             /    \            
  3.        (0-500)   (501-999)
  4.       /     \
  5. (0-250)  (251-500)
  6.           /      \
  7.   (251-375)      (376-500)
  8.                  /      \
  9.           (376-438)    (439-500)
  10.                         /     \
  11.                  (438-469)   (470-500)
  12.                              /      \
  13.                        (469-485)   (486-500)
  14.                                    /    \
  15.                             (485-493)  (494-500)
  16.                                         /    \
  17.                                  (493-497) (498-500)
  18.                                             /   \
  19.                                          (499)  (500)
  20.                                           |
  21.                                        (found)
  22.  
Sep 11 '17 #3

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

Similar topics

8
by: Hugh Sparks | last post by:
When processing an xml document that contains elements such as: <element xmlns="goop"/> ... <element xmlns="gleep"/> I want to create two xsl stylesheet templates: one that matches the...
1
by: lltaylor2000 | last post by:
Hello, I am creating an XML Document using XmlDocument. I can create my top node no problem However when I add child nodes they seem to inherit the xmlns="" attribute (see xml below). How...
5
by: CindyRob | last post by:
Using .NET framework 1.1 SP1, .NET framework SDK 1.1 SP1, Visual Studio .NET 2003, hotfixes 892202 and 823639. I create a proxy class using wsdl.exe, and in the serialized XML request, I see...
2
by: Noah Sussman | last post by:
Hello, I am writing a function to reposition an element based on whether one of its edges is outside the visible area of the browser window (the code is below). My client has asked for code...
25
by: Dave | last post by:
Hello. In trying to get an anchor element to stylistically match an input or button element, I find that the button and input cannot be styled according to the 2.1 CSS spec. For example, I...
1
by: P | last post by:
Hi, I am finding it little harder to get this done, kindly help me. I need to convert an XML file to a JavaScript file (Array) using XSLT, 1. Can I use <xsl:output method="text">? 2. I am...
6
by: rojelio | last post by:
I have a form with many fields... the fields are getting values from a database. I've tried onunload to just submit the form regardless of changes or not but onunload and submit isn't working...
1
by: boarderchic15 | last post by:
Hi- I have the following XML structure: <?xml version="1.0" encoding="UTF-8"?> <content> <type content_type="Listing"/> <faq_listing> <heading>Benefits</heading>...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.