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
Selection
37151325
37151325
37151325
37151325
37151325
27151335
27151335
27151335
27151335
27151335
23151375
23151375
23151375
23151375
23513715
23513715
23513715
23571315
23571315
Insertion
37151325
37151325
37151325
37131525
37131525
37131525
37132155
37132155
37213155
37213155
32713155
32713155
23713155
23713155
23713515
23713515
23751315
23751315
23571315
23571315
Bubble
37151325
37151325
37152135
37152135
37215135
37215135
32715135
32715135
23715135
23715135
23715513
23715513
23751513
23751513
23571513
23571513
23571513
23571315
23571315
23571315
23571315
23571315
23571315

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.

Quicksort
31971315217523
31971315217523
31971315217523
31971315217523
35713152171923
35713152171923
35713152171923
35713152171923
35713152171923
35713152171923
35713215171923
35713215171923
35713215171923
35713152171923
35713215171923

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.

Shellsort
371520131025
371551310220
371551310220
372513101520
372513101520
372513101520
372513101520
372513101520
352713101520
352713101520
253713101520
253713101520
235710131520
Heapsort
371520131025
371520131025
371520131025
320157131025
320157131025
320157131025
203157131025
203157131025
201315731025
513157310220
513157310220
151357310220
151357310220
151310735220
213107351520
213107351520
132107351520
132107351520
137102351520
571023131520
571023131520
107523131520
375210131520
375210131520
235710131520
Mergesort
371520131025
371520131025
371520131025
371520251013
371520251013
2
371520251013
23
371520251013
235
371520251013
2357
371520251013
235710
371520251013
235710131520