# Fall 2002 CSCI 333 Homework 4 Solution

## Problem 1

Show the comparisions and exchanges made by the bad sorts, selection, insertion, and bubble, on the following input set of six values:

1. 3
2. 7
3. 15
4. 13
5. 2
6. 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 3 7 15 13 2 5 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 7 15 13 3 5 2 3 15 13 7 5 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
 3 7 15 13 2 5 3 7 15 13 2 5 3 7 15 13 2 5 3 7 13 15 2 5 3 7 13 15 2 5 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 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
 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 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 2 3 5 7 15 13 2 3 5 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15 2 3 5 7 13 15

## Problem 2

Show how quicksort will partition the following array of nine values into two parts. Show the exchange and comparisions made in the partitioning. You may choose the pivot.

1. 3
2. 19
3. 7
4. 13
5. 15
6. 2
7. 17
8. 5
9. 23

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

 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 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 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 3 5 7 13 2 15 17 19 23

## Problem 3

Show how the good sorts, shellsort, heapsort, and mergesort will sort the following list of eight values.

1. 3
2. 7
3. 15
4. 20
5. 13
6. 10
7. 2
8. 5

Since insertion sort beats the O(n log n) sorts for small data sets, in our examples we'll assumer we're using insertion sort where there are only four unsorted elements remain.

 3 7 15 20 13 10 2 5 3 7 15 5 13 10 2 20 3 7 15 5 13 10 2 20 3 7 2 5 13 10 15 20 3 7 2 5 13 10 15 20 3 7 2 5 13 10 15 20 3 7 2 5 13 10 15 20 3 7 2 5 13 10 15 20 3 5 2 7 13 10 15 20 3 5 2 7 13 10 15 20 2 5 3 7 13 10 15 20 2 5 3 7 13 10 15 20 2 3 5 7 10 13 15 20
 3 7 15 20 13 10 2 5 3 7 15 20 13 10 2 5 3 7 15 20 13 10 2 5 3 20 15 7 13 10 2 5 3 20 15 7 13 10 2 5 3 20 15 7 13 10 2 5 20 3 15 7 13 10 2 5 20 3 15 7 13 10 2 5 20 13 15 7 3 10 2 5 5 13 15 7 3 10 2 20 5 13 15 7 3 10 2 20 15 13 5 7 3 10 2 20 15 13 5 7 3 10 2 20 15 13 10 7 3 5 2 20 2 13 10 7 3 5 15 20 2 13 10 7 3 5 15 20 13 2 10 7 3 5 15 20 13 2 10 7 3 5 15 20 13 7 10 2 3 5 15 20 5 7 10 2 3 13 15 20 5 7 10 2 3 13 15 20 10 7 5 2 3 13 15 20 3 7 5 2 10 13 15 20 3 7 5 2 10 13 15 20 2 3 5 7 10 13 15 20
 3 7 15 20 13 10 2 5 3 7 15 20 13 10 2 5 3 7 15 20 13 10 2 5 3 7 15 20 2 5 10 13 3 7 15 20 2 5 10 13 2 3 7 15 20 2 5 10 13 2 3 3 7 15 20 2 5 10 13 2 3 5 3 7 15 20 2 5 10 13 2 3 5 7 3 7 15 20 2 5 10 13 2 3 5 7 10 3 7 15 20 2 5 10 13 2 3 5 7 10 13 15 20