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.

37151325
37151325
37151325
37151325
37151325
27151335

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

27151335
27151335
27151335
27151335
23151375

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

23151375
23151375
23151375
23513715
23513715
23513715
23571315
23571315
23571315

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.

37151325

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.

37151325

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

37151325
37131525
37131525

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

37131525
37132155
37132155
37213155
37213155
32713155
32713155
23713155

One more long pass and we're done.

23713155
23713515
23713515
23751315
23751315
23571315
23571315

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.

37151325
37151325
37152135
37152135
37215135
37215135
32715135
32715135
23715135

Bubble up the second smallest.

23715135
23715513
23715513
23751513
23751513
23571513
23571513

Bubble up a third.

23571513
23571315
23571315
23571315

Do one more.

23571315
23571315

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

23571315

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.

31971315217523
31971315217523
31971315217523
31971315217523
35713152171923

Begin a second inward sweep followed by a swap

35713152171923
35713152171923
35713152171923
35713152171923
35713152171923
35713215171923

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

35713215171923
35713215171923
35713152171923

So we undo the last swap.

35713215171923

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.

37151325
37151325
31315725

Now we must run shiftdown on the root.

31315725
15133725
15133725
15135723

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.

31357215

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.

31357215
13357215
13357215
13753215

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

27531315

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

27531315
72531315
72531315
73521315
23571315

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.

23571315
53271315
23571315
23571315
32571315
23571315