

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CSC 101 â Section C Algorithms Examples to Try - Answers 11/12/
In class, we discussed sequential search and binary search. I talked about the order of growth of the two search methods: sequential search is O(n), and binary search is O(log n).
a) Which of the two approaches is considered a âdivide and conquerâ approach and why?
Binary search is the âdivide and conquerâ approach. It involves throwing away half of the remaining data (the âdivideâ component) after a comparison to an item in the list is made.
b) What property must our list have for us to be able to use binary search? Try to explain why that property needs to hold (that is, explain what binary search exploits to allow for a speedup in searching).
The list must be sorted already (for example: a list of names must be in alphabetical order). This allows us to throw away half of the remaining data after a comparison against one item in the list. If the last name we are looking for is before the last name in the middle of the alphabetized list, we know it is also before all names after the last name in the middle of the alphabetized list [precisely because we know they are in order].
c) Assume that you have 64 items to search through. What is the min and max number of items you may have to check if you are performing sequential search and if you are performing binary search (assuming the list has the property required for binary search indicated in part a). Feel free to use a calculator if you need to.
Min Max Sequential Search 1 (first item in list; first comparison)
64 (last item in list)
Binary Search 1 (middle item in list; first comparison)
6 (this is log 2 64)
a) Justify why a loop construct would be useful in implementing the sequential search algorithm.
Yes a loop construct would be useful in implementing sequential search. We need to compare the item we are searching for against every item in a list. The comparison process is the same every time (is my item equal to the item Iâm looking at in the list?) and we just need to repeat that comparison process until one of two things happen â we find the item doing a comparison, or we run out of things to compare against.
b) The powerpoint slide for looping says a loop will run for some period of time (maybe infinitely). Describe two situations you believe should cause a sequential search loop to stop.
The last sentence of part a gives the answer for this question. The stopping situations are that you either found the item (no need to continue searching) or you already compared against the whole list (no need to continue searching â thereâs nothing left you havenât already looked at).
State whether the binary search algorithm is something you could implement as a âfunctionâ. In your answer, make sure you define what a function is.
A function is a way to package up some programming code and refer to it by a convenient name. Succinctly, the answer is: Yes, binary search could be implemented as a function â as the algorithm is a block of code that we may want to put a label on.
[Extra thought question: if possible, would it be a good idea to implement binary search as a function would or would it not be useful?]
It would be a great idea â we might need to search lots of lists and functions allow us to implement an algorithm 1 time and then reuse it be referring to the label. Wake Forest probably has one binary search function implemented in WIN, with it being used to search through lists of names for different classes (each class will have a different list from every other).