Sie sind auf Seite 1von 2

CST 370 Summer B 2017

Homework 8
Due: 06/27/2017 (Tuesday) (11:55 PM)

Name:______________________

ID: _________________

1. Using the mergesort pseudocode in our textbook, sort the numbers 5, 2, 7, 4, 8, 6, 3 in the ascending
order. You should describe your answer as Figure 5.2 of our textbook. Note that our textbooks
example has 8 numbers. However, this problem has 7 numbers. Thus, you should read the
pseudocode carefully and identify how it sorts the 7 numbers.
[Note: If it is difficult to draw the operation on the word file, draw it on paper by hand. Then take a
picture of the paper and attach the picture here.]

2. Consider the quicksort algorithm covered in the class. Present the first partitioning operation for the
list 6 8 2 5 3 7 1 9 , You should use the first number, 6, as a pivot for the partitioning. In your
answer, you have to present the indexes i and j clearly.

3. Write a C++ program called performance.cpp that displays the performance of two different sorting
algorithms (= selection sort and quick sort) on the screen.

For the problem, your program should read the input size (= number of input data) and max value of input
data from a user. In the program, you should generate the input data (= integer numbers) using a random
number generator. The random numbers generated by your program should be in the range between zero
and the max value given by the user. For the homework, your program should save the input data to a file
called input.txt so that you can check the content of the input data later when you finish the execution.

Once your program generates the input data, it should conduct two sorting algorithms for the input file
and display the number of execution of the basic operation in the algorithms. In the case of the two
algorithms, a comparison operation will be the basic operation of each algorithm.

For the homework, you have to use dynamic array(s) to process the sorting data and the pseudocode
covered in the textbook. The selection sort is described in the page 99 and the quicksort is presented in
Chapter 5.2.

The following presents a sample run of the program. Your program should run like this.

Enter input size: 3000


Enter max value of input: 5

============================================================
Generate input data . . .
Done.

Page 1 of 2
============================================================
Selection sort performance
Number of executions: ???? times
============================================================
Quicksort performance
Number of executions: ???? times
============================================================

In the sample execution, your program should generate 3,000 integer numbers in the file input.txt. The
data in the file should have integer values between 0 and 5 because the user entered 5 as the max value of
the input data. This is a sample content of the file:
0 5 3 3 2 0 5 ... // It should have total 3,000 numbers.

In the sample execution, the instructor presents the number of executions as ???? times. But in your
real execution, your program should present an actual number such as 23519 times.

For the homework, its enough to display the number of executions of the basic operation in each
algorithm. Originally, the instructor considered to display elapsed time of each program. However, the
time measurement depends on the OS and machine you use. Thus, the instructor decided to display the
number of executions of the basic operation using a counter.

How to turn in? Write your answer to the question 1 and 2 using the Microsoft Word and submit it on the
iLearn. You should also submit your C++ file on the iLearn. Thus, you have to submit two files (one
MS-Word file and one C++ source file) on the iLearn.

Page 2 of 2

Das könnte Ihnen auch gefallen