Searching and sorting are at the core of most Computer Science classes becasue they allow us to reason about, illustrate, and analyze algorithms.
In this assignment let's explore two simple search algorithms.
Start with this snippet of code to generate an array of the numbers 1-999 in a random order:
values = (1..1000).to_a.shuffle
Write code to find a value x
(such as 25
) in the array following this algorithm:
- Set a
found
flag to false - Set a
marker
to zero - Find the value in
values
at positionmarker
- See if that value is equal to the one we're searching for
- If it is, set
found
to true - If it's not, do nothing
- If
marker
is at the end of the set, exit the search and say the value was not found - If the value has not been found, increment the
marker
and go back to step 2 - If the value has been found, print that message
Once you have that code written and working (make sure you try out several target search values), answer the questions below.
Imagine that comparing two numbers is the only instruction that is "expensive" in this algorithm.
- How many comparisons would run in the best-case scenario for that algorithm?
- How many comparisons would run in the worse-case scenario for that algorithm?
- How many comparisons would run in an average case?
- How would the worst-case scenario grow in proportion to the number of elements in the set?
Let's say that our set of values is shaped a little differently. Use this code to generate it:
values = (1..10000).to_a.sample(1000).sort
We now have a total of 1000 unique values between 1 and 10,000 and, most importantly, they're sorted.
Your challenge is to write a bisecting search. Here's the basic premise of the algorithm:
- Say you're searching for a value
t
in a search set ofvalues
- Find the middle value in
values
we'll callm
- If
t
is equal tom
then your target is found - If
t
is less thanm
, your new search set is the half ofvalues
beforem
- If
t
is greater thanm
, your new search set is the half ofvalues
afterm
- If your search set is now empty, the
t
is not found - If your search set is not empty, return to step 2 with that new set
When you think you have it working, make sure to try out some tricky cases like these:
- Search for the value that is right on the dividing line of a set
- Search for a value that is not found
- Search for a value that is the only value in a set
Let's do some analysis of the algorithm. But, fair warning, this will be much tougher than the first algorithm. Continue with the idea that comparing two numbers is the only instruction that is "expensive" in this algorithm.
- How many comparisons would run in the best-case scenario for that algorithm?
- How many comparisons would run in the worse-case scenario for that algorithm?
- How many comparisons would run in an average case?
- How would the worst-case scenario grow in proportion to the number of elements in the set?