# CSCI 333 Homework 4 Solution

## Problem 1

Show the comparisions and exchanges made by the bad sorts, selection and insertion, 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

## 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, 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 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

## Problem 4

A file contains a sequence of integers, possibly separated by commas. Write a C++ subroutine which is passed an `istream` that has been opened to this file and returns the sum of all the integers in the file. Return -1 is the file is not in the correct format.

Here is an example of the file input:

```   35  67, 89
13 5

13, 67
```
```int SumEm(istream NumS) {
int retSum = 0 ;
int nxtNum ;
while (!NumS.eof()) {
NumS >> nxtNum ;
if (!NumS.eof()) {
if (NumS.fail())
return -1 ;
else {
retSum += nxtNum ;
// keep reading integers until you get a comma or EOF
while (NumS >> nxtNum && !NumS.fail())
retSum += nxtNum ;
if (!NumS.eof()) {
NumS.clear() ;
// This better be a comma
char c = 'X' ;
NumS >> c ;
if (c != ',')
return -1 ;
}
}
}
}
return retSum ;
}
```

## Problem 5

Write some C++ statements to replace the fifth character of a file with the character 'X'.

```// Assume the file has been opened to ostream F
char OutBuff[1] = { 'X' } ;
F.seekp(5, ios::beg) ;
F.write(OutBuff, 1) ;
```