In Class Week7: Thursday

  1. Copy the file for today's class into your week7 subdirectory, then change into that directory and check the contents:
         % cd ~/cs21/
         % mkdir week7		# if you have not already done so
         % cd  week7
         % cp ~newhall/public/cs21/week7/bubblesort.c  .
         % cp ~newhall/public/cs21/week7/testinput  .
         % ls
    

  2. Open bubblesort.c program. You are going to implement and test the BubbleSort function. Bubble Sort works as follows:

    1. Make a pass through the array comparing adjacent bucket values as you go. If the adjacent values are in order do nothing, if they are not in sorted order relative to each other, then swap them.

    2. Continue making passes through the array until there are no swaps during a full pass (at this point the elements are in sorted order

    This algorithm is called Bubble Sort because values slowly bubble up to their correct position in the sorted array.

    For example, if the starting content of the array are the following:

       0     1     2      3     4     5    
     -------------------------------------
    |  7  |  6  |  5  |  4  |  3  |  2   |
     -------------------------------------
    
    Then, after the first pass of the bubble sort algorithm the array contains:
       0     1     2      3     4     5    
     --------------------------------------
    |  6  |  5  |  4  |  3  |  2  |  7   |
     --------------------------------------
    

    Try running and testing your implementation using an inputfile of input values and then re-directing a.out's stdin to read values from the inputfile. This is an easy way to test an interactive program so you don't have to enter them by hand each time. For example, my input file might contain:

    9
    10
    8
    33
    55
    88
    99
    77
    2
    6
    -1
    
    Then when I run bubble sort I can re-direct its input from this file by using < :
    	% ./a.out < inputfile
    

    You can see how long it takes to run bubble sort using the Unix time command:

    	% time  ./a.out < inputfile