Fall 2001 CSCI 333 Homework 5 Solution

Problem 1 -- O(n2) sorts

Selection sort

Begin the first sweep. It will select the smallest element in the array and put it in the first position.

 3 7 15 13 2 5 3 7 15 13 2 5 3 7 15 13 2 5 3 7 15 13 2 5 3 7 15 13 2 5 2 7 15 13 3 5

On the second pass, the second smallest element is selected.

 2 7 15 13 3 5 2 7 15 13 3 5 2 7 15 13 3 5 2 7 15 13 3 5 2 3 15 13 7 5

Now we complete. The passes are shorter as the number of number of unselected elements decreases.

 2 3 15 13 7 5 2 3 15 13 7 5 2 3 15 13 7 5 2 3 5 13 7 15 2 3 5 13 7 15 2 3 5 13 7 15 2 3 5 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15

Insertion sort

The first pass is very quickly. We put the first two elements in order. In this case, we don't even need to swap them.

 3 7 15 13 2 5

The second pass is just as quick. We know the first two elements are sorted. As soon as we see the third one is larger than the second we are done.

 3 7 15 13 2 5

This time we have to do a little work to move the fourth element into the correct position.

 3 7 15 13 2 5 3 7 13 15 2 5 3 7 13 15 2 5

This pass is even more work. We have to move the fifth element to the beginning of the array.

 3 7 13 15 2 5 3 7 13 2 15 5 3 7 13 2 15 5 3 7 2 13 15 5 3 7 2 13 15 5 3 2 7 13 15 5 3 2 7 13 15 5 2 3 7 13 15 5

One more long pass and we're done.

 2 3 7 13 15 5 2 3 7 13 5 15 2 3 7 13 5 15 2 3 7 5 13 15 2 3 7 5 13 15 2 3 5 7 13 15 2 3 5 7 13 15

Bubble sort

There are many variations of bubble sort. The one presented in the text has more than the usual number of flaws.

We begin by bubbling the smallest element to the first position of the array.

 3 7 15 13 2 5 3 7 15 13 2 5 3 7 15 2 13 5 3 7 15 2 13 5 3 7 2 15 13 5 3 7 2 15 13 5 3 2 7 15 13 5 3 2 7 15 13 5 2 3 7 15 13 5

Bubble up the second smallest.

 2 3 7 15 13 5 2 3 7 15 5 13 2 3 7 15 5 13 2 3 7 5 15 13 2 3 7 5 15 13 2 3 5 7 15 13 2 3 5 7 15 13

Bubble up a third.

 2 3 5 7 15 13 2 3 5 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15

Do one more.

 2 3 5 7 13 15 2 3 5 7 13 15

A real bubblesort would know it hadn't made any exchanges in the last pass and stop. But our bubblesort keeps going.

 2 3 5 7 13 15

Problem 2 -- Quicksort

We're going to choose the middle element, 15, as the pivot.

Move from the right and left, comparing elements while looking for two to swap.

 3 19 7 13 15 2 17 5 23 3 19 7 13 15 2 17 5 23 3 19 7 13 15 2 17 5 23 3 19 7 13 15 2 17 5 23 3 5 7 13 15 2 17 19 23

Begin a second inward sweep followed by a swap

 3 5 7 13 15 2 17 19 23 3 5 7 13 15 2 17 19 23 3 5 7 13 15 2 17 19 23 3 5 7 13 15 2 17 19 23 3 5 7 13 15 2 17 19 23 3 5 7 13 2 15 17 19 23

We perform a third sweep. This time we go too far and end up unneccesarily exchanging the ends of the pivot.

 3 5 7 13 2 15 17 19 23 3 5 7 13 2 15 17 19 23 3 5 7 13 15 2 17 19 23

So we undo the last swap.

 3 5 7 13 2 15 17 19 23

Problem 3 -- Heapsort

The first step is to build the heap containing the six elements. This will give us three interior nodes and three leaf nodes. We begin by calling `shiftdown` on the two second-level nodes. When we compare a node with its two immediate children, we show these two comparisions on one line.

 3 7 15 13 2 5 3 7 15 13 2 5 3 13 15 7 2 5

Now we must run `shiftdown` on the root.

 3 13 15 7 2 5 15 13 3 7 2 5 15 13 3 7 2 5 15 13 5 7 2 3

The array is now a maxheap. Switch the top element of the heap with the last value in the array. The last section of the array, only one element long, is now sorted.

 3 13 5 7 2 15

We must rebuild the heap by running `shiftdown` on the root. This time we don't consider the last element of the array as being in the heap.

 3 13 5 7 2 15 13 3 5 7 2 15 13 3 5 7 2 15 13 7 5 3 2 15

We have a new maxheap. It's time to move the second largest element of the array into place.

 2 7 5 3 13 15

Let's rebuild the heap and, once again, move its largest value to its rightful position.

 2 7 5 3 13 15 7 2 5 3 13 15 7 2 5 3 13 15 7 3 5 2 13 15 2 3 5 7 13 15

It's really silly to continue using heapsort at this point. It's past time to start using one of the O(n2) sorts, but we'll continue because it's so much fun.

 2 3 5 7 13 15 5 3 2 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15 3 2 5 7 13 15 2 3 5 7 13 15