Homework Assignment V Solution

1(6 points) Sort the array
74162385
using Bubble Sort . Show the results after each pass in the table below. Explain, briefly, what happened next to each row.

01234567index     
74162385original array
416237587 bubbles up to position 5
8 bubbles up to position 7
142365784 bubbles up to position 1
6 bubbles up to position 4
7 bubbles up to position 6
123456784 bubbles up to position 3
6 bubbles up to position 5
the array is now sorted
12345678these passes happen anyway
12345678these passes happen anyway
12345678these passes happen anyway
12345678these passes happen anyway

2(6 points) Sort the array
74162385
using Insertion Sort . Show the results after each pass in the table below. Explain, briefly, what happened next to each row.

01234567index     
74162385original array
47162385A[1] = 4 is sifted into place
14762385A[2] = 1 is sifted into place
14672385A[3] = 6 is sifted into place
12467385A[4] = 2 is sifted into place
12346785A[5] = 3 is sifted into place
12346785A[6] = 8 is sifted into place
(stays put)
12345678A[7] = 5 is sifted into place

3(6 points) Sort the array
74162385
using Selection Sort .
Show the results after each pass in the table below. Explain, briefly, what happened next to each row.

01234567index
74162385original array
147623851 swapped into position 0
127643852 swapped into position 1
123647853 swapped into position 2
123467854 swapped into position 3
123457865 swapped into position 4
123456876 swapped into position 5
123456787 swapped into position 6

4(8 points) Sort the array
74162385
using Quick Sort .
Select the pivot from the midpoint in the list. For each pass, show the results after each pass in the table below and indicate (on the right) all exchanges that move a corresponding pair of elements in the lower and upper sublist.

01234567index
74162385original array
54132687lower = 0 Upper = 7 pivot = 6
14532687lower = 0 Upper = 4 pivot = 1
14235687lower = 1 Upper = 4 pivot = 5
12435687lower = 1 Upper = 3 pivot = 2
12345687lower = 2 Upper = 3 pivot = 4
12345678lower = 5 Upper = 7 pivot = 8
12345678lower = 5 Upper = 6 pivot = 6


Last Updated: June 1, 1997 11:24 am by

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1101/HW/HW5/hw5Solution.html