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

Exercises

  1.   Consider the sequence of integers

    displaymath70134

    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

    displaymath70135

  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_inline66281. 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 five 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_inline70146. What is the maximum number of inversions that can be removed by the swapping of a pair of distinct elements tex2html_wrap_inline68750 and tex2html_wrap_inline68752? Express the result in terms of the distance between tex2html_wrap_inline68750 and tex2html_wrap_inline68752: tex2html_wrap_inline70156.
  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_inline59127 comparisons.
  10.   Consider an arbitrary sequence tex2html_wrap_inline70146. To sort the sequence, we determine the permutation tex2html_wrap_inline67486 such that

    displaymath70136

    Prove that bubble sort requires at least p passes where

    displaymath70137

  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_inline70168 with tex2html_wrap_inline70170 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. For example, 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_inline61267 largest element from an unsorted sequence of of n elements, tex2html_wrap_inline70186.
    1. If tex2html_wrap_inline70188, sort S and select directly the tex2html_wrap_inline61267 largest element.
    2. Otherwise n>5: Partition the sequence S into subsequences of length five. In general, there will be tex2html_wrap_inline70198 subsequences of length five and one of length tex2html_wrap_inline70200.
    3. Sort by any means each of the subsequences of length five. (See Exercise gif).
    4. Form the sequence tex2html_wrap_inline70202 containing the tex2html_wrap_inline70198 median values of each of the subsequences of length five.
    5. Apply the selection method recursively to find the median element of M. Let m be the median of the medians.
    6. Partition S into three subsequences, tex2html_wrap_inline70212. 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_inline70226 then apply the method recursively to select the tex2html_wrap_inline61267 largest element of L; if tex2html_wrap_inline70232, the result is m; otherwise apply the method recursively to select the tex2html_wrap_inline70236 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_inline59127.
  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 © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.