Assignment 1 (10 points)

Write three short programs that implement the Bubble Sort, Selection Sort, and Insertion Sort algorithms. Each of these programs will read up to 1000 decimal integers (expressed in text form), sort them, and then print them in a formatted fashion.

Your programs must properly handle cases where there are zero or more lines of text, each containing zero or more integer numbers separated by whitespace.

We will test your programs with input that containing zero integers, exactly 1000 integers, and several with in-between quantities. You may assume that the input data will only contain valid integer values.

For example, the following is a valid example of input data that contains 6 integers (with blank lines at the beginning, end, and in the middle):



109772

26443        32
12 -1 0



Manage your makefile and source files in this assignment in the same manner as for Assignemnt 0.

Input

Each program you write must read from standard input using input redirection.

It is your responsibility to create/provide a suitable set of data to debug your programs.

Files We Give You

You may use the makefile below to manage building this assignment:

#
# PROGRAM: sorting
# AUTHOR:  your name
#

CXXFLAGS+=-Wall -Werror -std=c++11

PROGS=bubble_sort selection_sort insertion_sort

.PHONY: all clean world

all: $(PROGS)

clean:
	rm -f *.o $(PROGS)

world: clean all

Using the above makefile, you can build one specific program or build all three programs. To build a single specific program, run the command make followed by the target name for the program that you want to build (bubble_sort, selection_sort, or insertion_sort). For example:

z123456@turing:~$ make bubble_sort

To build all three programs, run the command make all or just make.

Running the command make clean will remove all of the object and executable files created by the make command.

Running the command make world is the same as running make clean and then running make all.

Files You Must Write

You will write three files for this assignment:

The three files will obviously contain a great deal of identical code. It is advised that you factor the sorting logic appropriately so that it is easier to copy your first solution and then replace only the sort function(s).

Output

Each program you write must display its results on standard output.

The output should print 8 numbers per line (of course the last line may have less than 8 numbers), nicely aligned in columns. Numbers should be right-aligned. Use the std::setw() manipulator to pad numbers with leading spaces. (In the examples below, each number is padded out to 9 characters/spaces.)

There must not be any extra space after the last number and the newline character (\n) at the end of each line. The output should look like this:

z123456@turing:~/csci501/Assign1$ ./bubble_sort < test6.txt
       19       61       75       88       90       98
z123456@turing:~/csci501/Assign1$
z123456@turing:~/csci501/Assign1$ ./selection_sort < test16.txt
        7       15       15       20       30       37       42       52
       53       56       58       63       65       74       78       89
z123456@turing:~/csci501/Assign1$
z123456@turing:~/csci501/Assign1$ ./insertion_sort < test25.txt
        1       23      106      127      147      148      157      208
      239      265      282      483      561      576      579      677
      753      853      879      911      945      959      965      978
      982
z123456@turing:~/csci501/Assign1$

As is customary, your programs must always supply a newline character at the end of each line, even if the last line does not contain 8 numbers. In other words, after running your program, the Unix prompt must always appear on a line by itself, at the left side of the screen. The following is incorrect:

z123456@turing:~/csci501/Assign1$ ./bubble_sort < test6.txt
       19       61       75       88       90       98z123456@turing:~/csci501/Assign1$

Your programs must not emit any blank lines. The following is also incorrect (note the blank line after the last line is printed):

z123456@turing:~/csci501/Assign1$ ./selection_sort < test16.txt
        7       15       15       20       30       37       42       52
       53       56       58       63       65       74       78       89

z123456@turing:~/csci501/Assign1$

Handing In Your Assignment

To hand in your assignment you must follow the instructions in the Assignment Submission Instructions (as you did for Assignment 0.)

Hints

It may be easier to complete this assignment if you break it down into steps. Do one thing at a time, convince yourself that it works, and then move onto the next step. Below is an ordered sequence of steps you might want to try for this assignment.

  1. Decide which of the three sorting algorithms you're going to implement first. Selection sort is probably the easiest of the three to understand.

  2. Write code to read the values and print them out. Don't bother storing the values in an array at this step and don't bother formatting the output - just read the numbers and print them out, one per line. Make sure that all of the numbers are being read and that you're not skipping or printing the final number twice.

  3. Note: If the following example code is not obvious then you need to go back and review the the introductory readings in the course textbook!

    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
        int i;
        while (cin >> i)
        {
            cout << "Got '" << i << "'" << endl;
        }
    
        return 0;
    }
    
  4. Add code to declare an array and store the values read into that array. Instead of printing each number as it is read, write code to print the values after they've all been stored in the array. Once again, don't worry about formatting at this stage, just print one number one per line.

  5. Implement the pseudocode for your chosen sorting algorithm in C++. Test and debug it using input with a small number of values. Check your output using the sample output above.

  6. Format the output nicely.

  7. Make sure to test your program with larger amounts of data.

  8. Make sure to test your program with exactly 1000 input numbers!

  9. For the other two sorting algorithms, make a copy of your completed source file and then modify the sort code in the copy. The input and output code will not need to be modified.