2Chapter 19 Searching, Sorting and Big O
Self-Review Exercises
19.1 Fill in the blanks in each of the following statements:
a) A selection sort application would take approximately times as long to run
on a 128-element array as on a 32-element array.
b) The efficiency of merge sort is .
19.2 What key aspect of both the binary search and the merge sort accounts for the logarithmic
portion of their respective Big Os?
19.3 In what sense is the insertion sort superior to the merge sort? In what sense is the merge sort
superior to the insertion sort?
19.4 In the text, we say that after the merge sort splits the array into two subarrays, it then sorts
these two subarrays and merges them. Why might someone be puzzled by our statement that “it
then sorts these two subarrays”?
Exercises
NOTE: Solutions to the programming exercises are located in the ch19solutions folder.
Each exercise has its own folder named ex19_## where ## is a two-digit number representing
the exercise number. For example, Exercise 19.5’s solution is located in the folder ex19_05.
19.5 (Bubble Sort) Implement bubble sort—another simple yet inefficient sorting technique. It’s
called bubble sort or sinking sort because smaller values gradually “bubble” their way to the top of
the array (i.e., towards the first element) like air bubbles rising in water, while the larger values sink
to the bottom (end) of the array. The technique uses nested loops to make several passes through
the array. Each pass compares successive pairs of elements. If a pair is in increasing order (or the
values are equal), the bubble sort leaves the values as they are. If a pair is in decreasing order, the
bubble sort swaps their values in the array. The first pass compares the first two elements of the array
and swaps their values if necessary. It then compares the second and third elements in the array. The
end of this pass compares the last two elements in the array and swaps them if necessary. After one
pass, the largest element will be in the last index. After two passes, the largest two elements will be
in the last two indices. Explain why bubble sort is an O(n2) algorithm.