CS 210 Hour Exam Ideas

Things to think about.
   
    What is "big-O"?
    John calculated the time required for his algorithm as 50N + (N/2)N.  What is the "big-O" value he should quote?  Why?
   
    Mary calculated the cost of two algorithms, and reported one was 75N, the other (N/2)^2.   Which is faster?  Explain.
      Give an example of a case where Mary's first algorithm is better.  What are the relative times?
      Give an example where the second is better.   (or, better, in what region is the second better?)  Give an example of the relative times.
   
    Two sorts are both rated at O(N^2).  Yet one is "faster".  How can this be?  Explain.
   
    Be prepared to give the big-O value for things we've studied.
    Write a function to merge two ordered arrays.  Its output should be a new array.  Don't worry about duplicates.  Assume the contents of the arrays are of type int for convenience.
      i.e., provide the body for
        int[] merge (int[] A,  int[] B);
    ("merge" means that the output array will contain all the values in A and B, and be in order).  Your program should not care whether A and B are the same size, or even if one or both is of zero size.
   
    The same, but A and B are ordered lists  (class ListElt, fields "next" and "data").
   
    Write a function to verify that there are no duplicates in an ordered array.  What is the big-O value for this? Note that you do not have to remove the duplicates, just detect them.
    Same, but for an unordered array.
    Would it be better (faster) to first sort the array and then check? Explain. (the answer will probably have some big-O reasoning). Would your answer change if you had a O(n log n) sort available?
    Write an insertion sort for an array A of integers ...
   
    Consider an insertion sort which has made it part-way through the array, working left to right. What is the invariant that characterizes the data on the left (i.e., those elements A[k] where k is less than the current major loop index?)
    Both a selection sort and an insertion sort transfer values from the unsorted part of the array to other side. What is the difference between the two algorithms? (Hint: how many times is the data moved?)
   
  Alex created a queue of 8 elements (integers), using an array. Her program put data into the queue and took data out, in and out. Draw a picture of the array after 10 items have been added and 7 removed. (For simplicity, let's assume that the test program is adding sequential values, so that the first item added has the value 1, the second value is 2, etc.)
    In the previous question, Alex's program might have done a sequence like "A A R A A R ...". There are some sequences that could not occur. Explain. Perhaps you could describe an invariant which applies to the state of the queue at any time? Hint: you might try some examples, perhaps some very short ones. (Don't try listing all possible sequences: there are 17!/(10! * 7!) possibilities, which seems likely to be a rather large number!)
    Beth used a double-ended list to implement a queue, instead of an array, and did the same kind of testing as described above. One of the two error situations could not occur. Explain.
    Show a picture of Beth's list-based queue after adding 10 items and removing 7. (also: show a picture just after the queue has been created.)
    Consider Alex's queue program, but with a stack instead of a queue. How does the invariant change (the one describing which sequences are valid?) Can you draw a picture of the array after the additions and removals have been done? Explain.
   
  Given the stack calculator from the homework assignment, implement a factorial operator ("!"). Show the code which recognizes the operator, and the calculation of n!
   
   
    Additional material added Thurs am
    Show a recursive algorithm for finding the length of a list.
    Show a recursive algorithm for a binary search. How about a non-recursive one?