CSC 101 CompLit, Fall
1996
Merrie Bergmann
Ileana Streinu
Dominique Thiébaut
Algorithm = step by step method for problem solving.
What is a step?
Circuits implement one operation, such as: addition, comparison of numbers, access of one unit of data from the memory, etc.
Memory:
Analogies with students locations in the classroom: "address" = seat number.
Searching:
Sequential search
Let
n = number of elements in the list
L = the list of names that we are searching
e = the extra name that we are looking for in L.
The number of steps executed by the algorithm (sequential search)
is 4n if the extra name is not in the list (this is the worst
case).
Binary Search:
Method:
Steps 3 and 4 mean that you go back to step 1, and as long as
there are elements to look for in that part of the list, continue.
Binary search is MUCH more efficient
than sequential search: for example, if the list contains roughly
1K (1000) elements, sequential search performs the main loop 1000
times, but binary search only 10 times. (This approximation counts
only the number of comparisons done by the algorithm). On a list
of 1M (one million 1,000,000), binary search performs only 20
comparisons, in the worst case - as opposed to sequential search
which performs 1 million comparisons!
We say that binary search is more efficient than sequential search.
It pays to spend time to discover a good algorithm!
Next time we discuss in more detail the problem of
Given: a list L of n numbers
Want: to arrange L in (say)
increasing order.
Example: L = [ 6,2,5,8,1,4,3,7]
Sorted order: [1,2,3,4,5,6,7,8]
Method: (Selection Sort)
Next time: we'll look at other algorithms (methods) for sorting.