CS302 -- Lab 7


The purpose of this lab is to give you practice with sorting algorithms, b-trees, and extendible hashing. You should answer the first six questions using either an ascii text file or pdf file and you can answer the final two questions by hand drawing the diagrams or using a drawing editor. You should print out the answers to the first six questions and submit them, along with your diagrams for the final two questions, at the beginning of class on Tuesday, November 13. Please submit the answers to questions 1, 2, and 6 separately from the answers to questions 3, 4, 5, and 7. You should separately staple each set of answers.
  1. (20 points) For the following questions use this sequence of values:

    3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5

  2. (10 points) Show what the initial runs will look like for the following sequence if you use external sorting with replacement selection and a heap size of 4. Assume that you use 2-way merging:

    42, 53, 12, 65, 17, 3, 81, 9, 98, 1, 20, 15, 18, 66, 23, 71, 14, 43, 58, 16, 10, 75

    Use the same sort of table as I used in the homework 6 solutions to show me your intermediate work. However, for simplicity it is ok to simply show me the heap as an ordered array at each step, with an asterisk next to any values that belong in the next run. For example, it is ok to show your first, second, and third heaps as {12, 42, 53, 65}, {17, 42, 53, 65}, and {42, 53, 65, 3*}.

  3. (10 points) For the quicksort implementation with median-of-three partitioning, what is the running time when all the keys are equal? Justify your answer by showing me the proper recurrence relation and justify the recurrence relation.

  4. (20 points) Suppose you have an array of N elements, containing three distinct keys, true, false, and maybe. Give an O(N) algorithm to rearrange the array so that all false elements precede maybe elements, which in turn precede true elements. You may use only constant extra space, and definitely not O(N) extra space. This restriction precludes you from using bucket sort. Think instead about how you might modify the quicksort partitioning algorithm. You can use pseudo-code for your answer.

  5. (15 points) For each of the sorting algorithms state whether the algorithm is stable or unstable and explain why it is stable or unstable. Use three or fewer sentences in each of your explanations.

    1. quicksort
    2. insertion sort
    3. selection sort
    4. heap sort
    5. merge sort

  6. (10 points) For the following questions, choose the letter associated with the most appropriate algorithm:
         a. quicksort            d. bucket sort   h. replacement selection sort merge
         b. selection sort       e. merge sort    i. non-replacement selection sort merge
         c. insertion sort       f. heap sort
         

  7. (15 points)You are given the following initial B-tree with a maximum of 4 keys per interior node and a maximum of 5 records per leaf:
                                        _______________________
                                        |. 41 . 66 . 87 . -- .|
    		                    -----------------------
    	  |--------------------------|    |    |    |----------------------------
              |                       ---------    -------------                    |
              |                       |                        |                    |
    ______________________ _______________________ _______________________ _______________________
    |. 8 . 18 . 26 . 35 .| |. 48 . 51 . 54 . -- .| |. 72 . 78 . 83 . -- .| |. 92 . 97 . -- . -- .| 
    ---------------------- ----------------------- ----------------------- -----------------------
     |   |    |    |    |
     2   8   18   26   35
     4  10   20   28   36          ....                   ....                    ....
     6  12   22   30   37
        14   24   31   38
        16        32   39
    
    The contents of the leaf nodes for the last three interior nodes have been omitted because they do not affect the solution to this problem.

    1. Draw the tree that results when you insert 29 into the tree.
    2. What is the minimum number of keys that can be in an interior node, excluding the root?
    3. What is the minimum number of records that can be in a leaf?