Sample Data Structures Questions
Chapter 12
Sorting

Data Structures and Other Objects Using Java (Third Edition)
by Michael Main
ISBN 0-321-37525-4


The Purpose of These Questions

These are typical exam questions from Chapter 12 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) At the moment there are 13 short answer questions and 12 multiple choice questions in this file.

Short Answers

    Short Answers
    Section 12.1
    Quadratic Sorting
    Algorithms

  1. Here is an array of ten integers:
          5  3  8  9  1  7  0  2  6  4
    
    Draw this array after the FIRST iteration of the large loop in a selection sort (sorting from smallest to largest).

  2. Here is an array of ten integers:
          5  3  8  9  1  7  0  2  6  4
    
    Draw this array after the FIRST iteration of the large loop in an insertion sort (sorting from smallest to largest). This iteration has shifted at least one item in the array!

  3. Suppose that you are writing a program that has this selectionsort static method available:
        void selectionsort(int[ ] data, int first, int n);
    
    Your program also has an integer array called x, with 10 elements. Write two method activations: The first activation uses selectionsort to sort all of x; the second call uses selectionsort to sort x[3]..x[9].

    Short Answers
    Section 12.2
    Recursive Sorting
    Algorithms

  4. Describe a case where quicksort will result in quadratic behavior.

  5. Here is an array which has just been partitioned by the first step of quicksort:
          3, 0, 2, 4, 5, 8, 7, 6, 9
    
    Which of these elements could be the pivot? (There may be more than one possibility!)

  6. Give a concise accurate description of a good way for quicksort to choose a pivot element. Your approach should be better than "use the entry at location [0]".

  7. Give a concise accurate description of a good way for quicksort to improve its performance by using insertionsort.

  8. Here is an array of ten integers:
          5  3  8  9  1  7  0  2  6  4
    
    Suppose we partition this array using quicksort's partition function and using 5 for the pivot. Draw the resulting array after the partition finishes.

  9. Here is an array of ten integers:
          5  3  8  9  1  7  0  2  6  4
    
    Draw this array after the TWO recursive calls of merge sort are completed, and before the final merge step has occured.

  10. Implement the following static method:
        private static void merge(int[ ] data, int first, int n1, int n2);
        // Precondition: data has at least n1+n2 components starting at
        // data[first]. The first n1 elements (from data[first] to 
        // data[first+n1-1]) are sorted from smallest to largest, and the last
        // n2 elements (from data[first+n1] to data[first+n1+n2-1]) are also
        // sorted from smallest to largest.
        // Postcondition: Starting at data[first, n1+n2 elements of data have been
        // rearranged to be sorted from smallest to largest.
    

    Short Answers
    Section 12.3
    An O(n log n) Algorithm
    Using a Heap

  11. Write two or three clear sentences to describe how a heapsort works.

  12. Fill in the following table for the times to sort an array of n items. Use only big-O notation, and do not have any extraneous constants in your expressions.
    Worst Case Average Case
    Binary search of a sorted array ..
    Insertion sort ..
    Merge sort ..
    Quick sort without "median of three" pivot selection ..
    Quick sort with "median of three" pivot selection ..
    Selection sort ..
    Heap sort ..

  13. (This question may not have been covered in your class.) Suppose that you implement quicksort nonrecursively using a stack, as in your last programming assignment. You use your algorithm to sort an array of 100 items, and at the start of the final iteration of the while loop, the stack contains just two numbers: 10 (on top) and 90 (on bottom). Write one or two clear sentences to describe which parts of the array are sorted at this point, and which parts of the array remain to be sorted.

Multiple Choice

    Multiple Choice
    Section 12.1
    Quadratic Sorting
    Algorithms

  1. In a selectionsort of n elements, how many times is the swap function called in the complete execution of the algorithm?

  2. Selectionsort and quicksort both fall into the same category of sorting algorithms. What is this category?

  3. Suppose that a selectionsort of 100 items has completed 42 iterations of the main loop. How many items are now guaranteed to be in their final spot (never to be moved again)?

  4. Suppose we are sorting an array of ten integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are ordered as shown here:
        1  2  3  4  5  0  6  7  8  9
    
    Which statement is correct? (Note: Our selectionsort picks largest items first.)

  5. Suppose we are sorting an array of eight integers using a some quadratic sorting algorithm. After four iterations of the algorithm's main loop, the array elements are ordered as shown here:
        2  4  5  7  8  1  3  6
    
    Which statement is correct? (Note: Our selectionsort picks largest items first.)

  6. When is insertionsort a good choice for sorting an array?

    Multiple Choice
    Section 12.2
    Recursive Sorting
    Algorithms

  7. What is the worst-case time for mergesort to sort an array of n elements?

  8. What is the worst-case time for quicksort to sort an array of n elements?

  9. Mergesort makes two recursive calls. Which statement is true after these recursive calls finish, but before the merge step?

  10. Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this:
        2  5  1  7  9  12  11  10
    
    Which statement is correct?

  11. What is the worst-case time for heapsort to sort an array of n elements?

    Multiple Choice
    Section 12.3
    An O(n log n) Algorithm
    Using a Heap

  12. Suppose we are sorting an array of eight integers using heapsort, and we have just finished one of the reheapifications downward. The array now looks like this:
        6  4  5  1  2  7  8
    
    How many reheapifications downward have been performed so far?



Michael Main (main@colorado.edu)