CS 112 B1 - Spring 2003: Homework 5

Due Thursday, Apr 10 at 11:59 P.M.

Please keep in mind the policy on collaboration, as described on the syllabus.

Your code will be graded for style as well as correctness and should conform to the following style guidelines. Style will be worth 10 points total.


PROBLEM 1:

(50 points)

You are given a collection of template functions for inputting, outputting, and sorting an array of some class. You are also given a main() function that uses these templates on the integers. The current sort that's implemented is Bubble Sort. Implement function templates SelectionSortArray and QuickSortArray, in sort.h, analogously to bubble sort (in particular, use the swap function when needed, instead of swapping directly inside your sort using temporary variables--it'll make sense in the next problem). Test them out using main. A sample input file is included, but you should test on other inputs, as well. See comments inside sort.h to understand the proper input/output formats. For quicksort, use (for now, you'll change that later) the element in the lowest position as the pivot. The Ryba and Kruse textbook does a fairly good job explaining quicksort, you may use it (including pieces of code from the textbook, if you want, but note that the textbook uses a totally different way of organizing the code, so you'll have to modify it).

PROBLEM 2:

(30 points)

To get practice using templates, you will now use the same functions for sorting polynomials. We are providing you with the polynomial class, but it needs slight modifications: it needs friend operators >> and << for input and output, a friend function swap, and a member operator >. We provide >> and swap, and you will have to write the rest. When writing operator> (which is defined as follows: p1>p2 if degree of p1 is greater than degree of p2 or, if the degrees are equal, if the highest coefficient of p1 is greater than that of p2; if those are also equal, the answer is determined by the next terms, and so on), also add code to count the number of comparisons, analgously to how the number of swaps is counted (note the declaration and use of swapCount in polynomial.h and its definition initialization and increments in polynomial.cpp). If you do this correctly, you will be able to compile and link it together with polysort.cpp. You can now test the sorting of polynomials, and see how many comparisons and swaps your sort takes. A sample input and output files are included, but you should test on other inputs, as well. See comments inside polynomial.cpp to understand the proper input/output formats.

Try to find a sample input that causes a large number of comparisons for quicksort (as large as for selection sort), because you are always picking the lowest element as the pivot.

PROBLEM 3:

(10 points)

Create a new function, RandomizedQuickSortArray, which is just like your QuickSortArray but uses a random pivot (it may be easiest to swap it with the lowest element first). To get a random number between 0 and x-1, use rand() % x to take a random number returned by rand() modulo x (the function prototype for rand() is in cstdlib). Now your bad input from the previous problem is no longer bad.


Submit directory hw5 with the files sort.h, polynomial.h and polynomial.cpp. Do NOT change the input/output formats, or you will lose signigicant amounts of points.