© A+ Computer Science - www.apluscompsci.com
Please pick up your Recursion test.
You are going to implement a guessing game soon.
If you ask the user to guess a number between 1 and 1000 and your program gives the user feedback after each guess that is not correct (too high, too low), how many attempts should you give the user to ensure that the user can win as long as they make smart guesses?
Explain why in your IN.
To do: September 7
© A+ Computer Science - www.apluscompsci.com
1. What is a requirement of the list of numbers in order to use binary search?
2. What’s the other type of search algorithm called and what is its runtime?
Binary Search
© A+ Computer Science - www.apluscompsci.com
Why? You are decreasing the size of the list by half after each guess if you guess the middle number.
2. What is a requirement of the list of numbers in order to use this method of searching for a number?
List must be in order.
Binary Search
© A+ Computer Science - www.apluscompsci.com
Describe a case when you would use linear search instead of binary search.
Binary vs Linear Search
© A+ Computer Science - www.apluscompsci.com
boolean isHungry, isTired;
// assignment of variables not shown
What is equivalent to the following compound boolean expression?
!(isHungry || isTired) == ?
!(isHungry && isTired) == ?
Compound Boolean Expressions
© A+ Computer Science - www.apluscompsci.com
boolean isHungry, isTired;
// assignment of variables not shown
What is equivalent to the following compound boolean expression?
DeMorgan’s Law
!(isHungry || isTired) ==
!isHungry && !isTired
!(isHungry && isTired) ==
!isHungry || !isTired
Compound Boolean Expressions
© A+ Computer Science - www.apluscompsci.com
�
Go over test
© A+ Computer Science - www.apluscompsci.com
Implement binary search using iteration on the white board.
Once you're done, copy in your IN.
public int binarySearch (int [] nums, int val)
{
}
Binary search: iterative
© A+ Computer Science - www.apluscompsci.com
Homework