• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • SORTING

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 9

SORTING - all with Video Answers

Educators


Chapter Questions

01:36

Problem 1

Many operations can be performed faster on sorted than on unsorted data. For which of the following operations is this the case?
a. checking whether one word is an anagram of another word, e.g., plum and lump
b. finding an item with a minimum value
c. computing an average of values
d. finding the middle value (the median)
e. finding the value which appears most frequently in the data

Clarissa Noh
Clarissa Noh
Numerade Educator

Problem 2

The function bubblesort() is inefficient because it continues execution after an array is sorted by performing unnecessary comparisons. Therefore, the number of comparisons in the best and worst cases is the same. The implementation can be improved by making a provision for the case when the array is already sorted. Modify bubblesort () by adding a flag to the outer for loop indicating whether or not it is necessary to make the next pass. Set the flag to true every time an interchange occurs, which indicates that there is a need to scan the array again.

Check back soon!
01:37

Problem 3

Will bubblesort () work properly if the inner loop
for (int $j=n-1 ; j>i ;--j$ )
is replaced by
for (int $j=n-1 ; j>0 ;--j$ )
What is the complexity of the new version?

Manik Pulyani
Manik Pulyani
Numerade Educator

Problem 4

In our implementation of bubble sort, a sorted array was scanned bottom-up to bubble up the smallest element. What modifications are needed to make it work topdown to bubble down the largest element?

Check back soon!
10:41

Problem 5

A cocktail shaker sort designed by Donald Knuth is a modification of bubble sort in which the direction of bubbling changes in each iteration: In one iteration, the smallest element is bubbled up; in the next, the largest is bubbled down; in the next, the second smallest is bubbled up; and so forth. Implement this new algorithm and explore its complexity.

Tarandeep Singh
Tarandeep Singh
Numerade Educator
01:31

Problem 6

Insertion sort goes sequentially through the array when making comparisons to find a proper place for an element currently processed. Consider using binary search instead and give a complexity of the resulting insertion sort.

Nick Johnson
Nick Johnson
Numerade Educator

Problem 7

Draw decision trees for all the elementary sorting algorithms as applied to the array $[\mathrm{ab} c \mathrm{~d}]$.

Check back soon!

Problem 8

Which of the algorithms discussed in this chapter is easily adaptable to singly linked lists? To doubly linked lists?

Check back soon!
08:52

Problem 9

What exactly are the smallest and largest numbers of movements and comparisons to sort four elements using heapsort (), quicksort (), and mergesort ()?

Bobby Barnes
Bobby Barnes
University of North Texas

Problem 10

Implement and test mergesort().

Check back soon!
01:28

Problem 11

Show that for mergesort the number of comparisons $C(n)=n \lg n-2^{\lg n}+1$.

James Kiss
James Kiss
Numerade Educator

Problem 12

Implement and analyze the complexity of the following nonrecursive version of mergesort. First, merge subarrays of length 1 into $\frac{n}{2}$ two-cell subarrays, possibly one of them being a one-cell array. The resulting arrays are then merged into $\frac{1 t}{4}$ four-cell subarrays possibly, with one smaller array, having one, two, or three cells, etc., until the entire array is ordered. Note that this is a bottom-up approach to the mergesort implementation as opposed to the top-down approach discussed in this chapter.

Check back soon!

Problem 13

mergesort () merges the subarrays of an array that is already in order. Another topdown version of mergesort alleviates this problem by merging only runs, subarrays with ordered elements. Merging is applied only after two runs are determined. For example, in the array $[67834101112132 \mid$, runs $[678]$ and $|34|$ are first merged to become $\left[34678 \mid\right.$, then runs $\left[\begin{array}{llll}10 & 11 & 12 & 13\end{array} \mid\right.$ and $[2]$ are merged to become $[2101]$ $1213 \mid$, and finally, runs $[34678 \mid$ and $|210111213|$ are merged to become $[2,34$ 7810111213 . Implement this algorithm and investigate its complexity. A mergesort that takes advantage of a partial ordering of data (that is, uses the runs) is called a natural sort. A version that disregards the runs by always dividing arrays into (almost) even sections is referred to as straight merging.

Check back soon!

Problem 14

To avoid doubling the workspace needed when arrays are sorted with mergesort, it may be better to use a linked list of data instead of an array. In what situations is this approach better? Implement this technique and discuss its complexity.

Check back soon!

Problem 15

Which sorting algorithms are stable?

Check back soon!

Problem 16

Consider a slow sorting algorithm, which applies selection sort to every th element of an $n$-element array, where $i$ takes on values $n / 2, n / 3, \ldots, n / n$ (Julstrom 1992). First, selection sort is applied to two elements of the array, the first and the middle element, then to three elements, separated by the distance $n / 3$, etc., and finally, to every element. Compute the complexity of this algorithm.

Check back soon!