Docsity

Assignment on Algorithms Example | Overview of Computer Science | CSC 101, Assignments of Computer Science

Toggle sidebar
bg1
CSC 101 – Section C
Algorithms Examples to Try - Answers
11/12/2007
1. [This question should reinforce your understanding of the difficulty of searching, of
two common ‘algorithms’ for searching a list, and what order of growth means in
computer science].
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 log264)
2. [This question should reinforce your understanding of the ‘looping’ program
construct, as well as your understanding of searching algorithms.]
a) Justify why a loop construct would be useful in implementing the sequential search
algorithm.
pf2

Partial preview of the text

Download Assignment on Algorithms Example | Overview of Computer Science | CSC 101 and more Assignments Computer Science in PDF only on Docsity!

CSC 101 – Section C Algorithms Examples to Try - Answers 11/12/

  1. [This question should reinforce your understanding of the difficulty of searching, of two common ‘algorithms’ for searching a list, and what order of growth means in computer science].

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)

  1. [This question should reinforce your understanding of the ‘looping’ program construct, as well as your understanding of searching algorithms.]

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).

  1. [This question should reinforce your understanding of the definition of a function, as well as your understanding of the concept of binary search].

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).