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

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

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

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) ;