CSC 101 CompLit, Fall 1996
Merrie Bergmann
Ileana Streinu
Dominique Thiébaut

Lecture 19

Tuesday, November 12,1996

The GREAT IDEAS behind everything:

ALGORITHMS

Algorithm = step by step method for problem solving.


  1. Steps: computers execute one operation at a time.
  2. Where is the information being processed stored, and how? -> Memory of the computer.
  3. Problems. Examples: searching and sorting.
  4. Methods for solving these problems in a step by step fashion: algorithms for searching and sorting.
  5. Animations of algorithms (next time).

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.


PROBLEMS

Searching:


Methods

Sequential search

  1. take each elements of the list, one after another (as long as there are any elements left).[one memory access for each]
  2. compare the current element from the list with the extra name [one operation: comparison]
  3. if they are equal, STOP and say "YES".
  4. if not, continue from step 1.

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:

  1. list of names is sorted (arranged in alphabetical order).

Method:

  1. compare e with the middle element in the list L.
  2. if it is equal, STOP and say "YES".
  3. if it is less than the middle element, then continue searching the first half of the list.
  4. if it is larger than the middle element, then continue searching the second half of the list.

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

Sorting.

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)

  1. Find the minimum element of the list L.
  2. write it at the end of the answer list.
  3. remove it from L
  4. as long as there are still elements in L, continue from step 1.

Next time: we'll look at other algorithms (methods) for sorting.