Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
beginner help with sequential and binary search
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="popeyerayaz, post: 3586730"] 2. Consider the array consisting of 7 elements sorted in ascending order. What is the Maximum number of comparisons that need to be done to find any given element using: a. Sequential search (10 points) b. Binary search (10 points) Answer below: a. Since the total number of elements is seven, and the value is unknown I can only assume that the maximum number of comparisons may be the total amount of elements, seven (7). b. The binary search for any given number is what I can’t figure out. I have attempted to use the following code and formula but am very confused as to what the answer could be. I can get certain values but when I try for example the number two as the value to search for it just doesn’t work. low = 0 high = N while (low < high) { mid = (low + high)/2; if (A[mid] < value) low = mid + 1; else //can't be high = mid-1: here A[mid] >= value, //so high can't be < mid if A[mid] == value high = mid; } if (low < N) and (A[low] == value) return low else return not_found any help greatly appreciated. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
beginner help with sequential and binary search
Top