Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Exercises

  1.   Consider the sequence of integers

    displaymath71237

    For each of the following sorting algorithms, draw a sequence of diagrams that traces the execution of the algorithm as it sorts the sequence S: straight insertion sort, binary insertion sort, bubble sort, quick sort, straight selection sort, heapsort, merge sort, and bucket sort.

  2.   Draw a sequence of diagrams that traces the execution of a radix-10 sort of the sequence

    displaymath71238

  3. For each of the sorting algorithms listed in Exercises gif and gif indicate whether the sorting algorithm is stable.
  4. Consider a sequence of three distinct keys tex2html_wrap_inline67128. Draw the binary decision tree that represents each of the following sorting algorithms: straight insertion sort, straight selection sort, and bubble sort.
  5.   Devise an algorithm to sort a sequence of exactly three elements. Make your algorithm as efficient as possible.
  6. Prove that the swapping of a pair of adjacent elements removes at most one inversion from a sequence.
  7. Consider the sequence of elements tex2html_wrap_inline71249. What is the maximum number of inversions that can be removed by the swapping of a pair of distinct elements tex2html_wrap_inline69747 and tex2html_wrap_inline69749? Express the result in terms of the distance between tex2html_wrap_inline69747 and tex2html_wrap_inline69749: tex2html_wrap_inline71259.
  8. Devise a sequence of keys such that exactly eleven inversions are removed by the swapping of one pair of elements.
  9. Prove that binary insertion sort requires tex2html_wrap_inline59897 comparisons.
  10.   Consider an arbitrary sequence tex2html_wrap_inline71249. To sort the sequence, we determine the permutation tex2html_wrap_inline68485 such that

    displaymath71239

    Prove that bubble sort requires at least p passes where

    displaymath71240

  11. Modify the bubble sort algorithm (Program gif) so that it terminates the outer loop when it detects that the array is sorted. What is the running time of the modified algorithm? Hint: See Exercise gif.
  12. A variant of the bubble sorting algorithm is the so-called odd-even transposition sort . Like bubble sort, this algorithm a total of n-1 passes through the array. Each pass consists of two phases: The first phase compares tex2html_wrap_inline71271 with tex2html_wrap_inline71273 and swaps them if necessary for all the odd values of of i. The second phase does the same for the even values of i.
    1. Show that the array is guaranteed to be sorted after n-1 passes.
    2. What is the running time of this algorithm?
  13. Another variant of the bubble sorting algorithm is the so-called cocktail shaker sort . Like bubble sort, this algorithm a total of n-1 passes through the array. However, alternating passes go in opposite directions. E.g., during the first pass the largest item bubbles to the end of the array and during the second pass the smallest item bubbles to the beginning of the array.
  14.   Consider the following algorithm for selecting the tex2html_wrap_inline61700 largest element from an unsorted sequence of of n elements, tex2html_wrap_inline71289.
    1. If tex2html_wrap_inline71291, sort S and select directly the tex2html_wrap_inline61700 largest element.
    2. Otherwise n>5: Partition the sequence S into subsequences of length five. In general, there will be tex2html_wrap_inline71301 subsequences of length five and one of length tex2html_wrap_inline71303.
    3. Sort by any means each of the subsequences of length five. (See Exercise gif).
    4. Form the sequence tex2html_wrap_inline71305 containing the tex2html_wrap_inline71301 median values of each of the subsequences of length five.
    5. Apply the selection procedure recursively to find the median element of M. Let m be the median of the medians.
    6. Partition S into three subsequences, tex2html_wrap_inline71315. such that all the elements in L are less than m, all the elements in E are equal to m, and all the elements of G are greater than m.
    7. If tex2html_wrap_inline71329 then apply the procedure recursively to select the tex2html_wrap_inline61700 largest element of L; if tex2html_wrap_inline71335, the result is m; otherwise apply the procedure recursively to select the tex2html_wrap_inline71339 largest element of G.
    1. What is the running time of this algorithm?
    2. Show that if we use this algorithm to select the pivot the worst-case running time of quick sort is tex2html_wrap_inline59897.
  15.   Show that the sum of the heights of the nodes in a complete binary tree with n nodes altogether is n-b(n), where b(n) is the number of ones in the binary representation of n.

next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.