next up previous
Next: About this document Up: My Home Page

SUNY at Stony BrookMidterm 2
CSE 214 - Data Structures November 21, 1997

Midterm Exam

Name: Signature:
ID #: Section #:

INSTRUCTIONS:

tabular33

1) (20 points) Show the state of the array after each pass by the following sorting routines. You do not have to show the array after every move or comparison, but only after each execution of the main sorting loop or recursive call. Sort in increasing order.

     -------------------------------------------------------------
     |  34 | 125 |   5 |  19 |  87 | 243 |  19 |  -3 | 117 |  36 |
     -------------------------------------------------------------

(a) Insertion Sort

10 points

     -------------------------------------------------------------
    |  34 | 125 |   5 |  19 |  87 | 243 |  19 |  -3 | 117 |  36 |
    |  34 \ 125 |   5 |  19 |  87 | 243 |  19 |  -3 | 117 |  36 |
    |  34 | 125 \   5 |  19 |  87 | 243 |  19 |  -3 | 117 |  36 |
    |   5 |  34 | 125 \  19 |  87 | 243 |  19 |  -3 | 117 |  36 |
    |   5 |  19 |  34 | 125 \  87 | 243 |  19 |  -3 | 117 |  36 |
    |   5 |  19 |  34 |  87 |  125\ 243 |  19 |  -3 | 117 |  36 |
    |   5 |  19 |  34 |  87 |  125| 243 \  19 |  -3 | 117 |  36 |
    |   5 |  19 |  19 |  34 |  87 |  125| 243 \  -3 | 117 |  36 |
    |  -3 |   5 |  19 |  19 |  34 |  87 |  125| 243 \ 117 |  36 |
    |  -3 |   5 |  19 |  19 |  34 |  87 |  117| 125| 243  \  36 |
    |  -3 |   5 |  19 |  19 |  34 |  36 |  87 | 117|   125| 243 |
    -------------------------------------------------------------

(b) Quicksort (pivot on rightmost element)

10 points - there are many variants

     ------------------------------------------------------------
    |  34 | 125 |   5 |  19 |  87 | 243 |  19 |  -3 | 117 |  36 |
    | 34  |  5  | 19  | 19  |  -3 |  36 | 125 |  87 | 243 | 117 |
    |  -3 | 34  |  5  | 19  |  19 |  36 | 87  | 117 | 125 | 243 |
    |  -3 |  5  |  19 | 19  |  34 |  36 | 87  | 117 | 125 | 243 |
    |  -3 |   5 |  19 |  19 |  34 |  36 |  87 | 117|   125| 243 |
    -------------------------------------------------------------

2) (25 points) In class, we discussed two different heuristics for self-organizing sequential search. With the move-to-front heuristic, on a query we perform a sequential search for the appropriate element, which when found is moved from its current position to the front of the list. With the move-forward-one heuristic, on a query we perform a sequential search for the appropriate element, which when found is moved one element closer to the front of the list. For both algorithms, if the search key is already at the front of the list, no change occurs.

(a) Write a function to implement sequential search in a linked list, with the move-to-front heuristic. You may assume that there are at least two elements in the list and that the item is always found.

PROCEDURE ListSearch (VAR p : pointer) : pointer =

var q, head:pointer;

head := p;
q := p;

if (q^.info = key) then
        return(q);
else
       	p := p^.next;
        while (p^.info # key) do
             p := p^.next;
             q := q^.next;
        end
        q^.next := p^.next;
        p^.next := head;
        head := p;
        return (p);
end
points for searching, 10 points for move to front.

(b) Which of these two heuristics is better suited for implementation with arrays? Why?

5 points

move-forward-one is better for arrays since it can be done via one swap.

3) (15 points) Assume you have an array with 11 elements that is to be used to store data as an hash table. The hash function computes the number mod 11. Given the following list of insertions to the table:

 2  4  13  18  22  31  33  34  42  43  49
Show the resulting table after the insertions for each of the following hashing collision handling methods.

a) Show the resulting table after the insertions for chaining. (array of linked lists)

10 points

0 - 22, 33 1 - 34 2 - 2, 13 3 - 4 - 4 5 - 49 6 - 7 - 18 8 - 9 - 31, 42 10 - 43

b) List an advantage and a disadvantage of chaining compared to open addressing

5 points.

Advantages - deletion is easier and hash table cannot be filled.

Disadvantages - the links use up memory which can go to a bigger hash table.

4) (20 points) Write brief essays answering the following questions. Your answer must fit completely in the space allowed

(a) Is f(n) = O(g(n)) if tex2html_wrap_inline62 and tex2html_wrap_inline64 ? Show why or why not. points

No! There is no constant such that tex2html_wrap_inline66 .

(b) Consider the following variant of insertion sort. Instead of using sequential search to find the position of the next element we insert into the sorted array, we use a binary search. We then move the appropriate elements over to create room for the new insertion. What is the worst case number of element comparisons performed using this version of insertion sort on n items (big Oh)? points

tex2html_wrap_inline70

(c) What is the worst case number of element movements performed using the above version of insertion sort on n items (big Oh)?

6 points

tex2html_wrap_inline74

I took off more points for inconsistancies between the answers...

5) (20 points) The integer square root of integer n (SQRT(n)) is the largest integer x such that tex2html_wrap_inline82 . For example, SQRT(8) = 2, while SQRT(9) = 3.

Write a Modula-3 function to compute SQRT(n). For full credit, your algorithm should run in tex2html_wrap_inline90 time. Partial credit will be given for an tex2html_wrap_inline92 algorithm.

(Hint: think about the ideas behind binary search)

    PROCEDURE sqrt(n : INTEGER):INTEGER =

var
    low, high, mid : integer;

low := 1;
high := n;

while (high - low) > 1 do
    mid := (high+low) div 2;
    if (mid * mid) > n then low := mid+1;
                       else high := mid;
end;

return (mid);
end;




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Wed Nov 26 00:21:51 EST 1997