In Class Week8: Tuesday

  1. Copy the files for today's class into your week8 directory, then change into that directory and check the contents:
         % cd ~/cs21/
         % mkdir week8
         % cd  week8
         % cp ~newhall/public/cs21/week8/*  .
         % ls

  2. Open selectionsort.c program. You are going to implement and test the SelectionSort function. Selection Sort works by iteratively finding the value that should go in each bucket in sorted order. It works as follows:

    1. Find the value that should go in bucket 0 by making a pass through the array buckets to find the bucket containing the smallest value. Swap the value in bucket 0 with the bucket holding the smallest value.

    2. Find the value that should go in bucket 1 by searching through the array from bucket 1 to find the bucket containing the smallest value. Swap this bucket's value with bucket 1's value.
    3. And so on,

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

       0     1     2     3     4     5    
    |  7  |  3  |  5  |  6  |  2  |  3   |
    Then, after the first pass of the Selection Sort algorithm, the array contents are:
       0     1     2     3     4     5    
    |  2  |  3  |  5  |  6  |  7  |  3   |

    Try running and testing your implementation by re-directing your program's input to the testinput file:

    	% ./a.out < testinput

  3. Open up the binarysearch.c and linearsearch.c files in emacs (or vi). Examine the search code and make sure you understand what each is doing.

    1. Compile the code. This code uses a Makefile to build the executables. Just type make to build the binary and linear executable files (make clean will remove all .o and executable files).

    2. To get some idea of which is faster, you can use the Unix time command to measure the amount of time each program takes to execute:
      	% time binary 
      	% time linear 
      	0.170u 0.010s 0:00.19 94.7%     0+0k 0+0io 129pf+0w
      		      this value is the total time it took to run (19 seconds) 
    3. Now lets see what happens when we change the size of the array. In search.h, increase the size of MAX_VALUES by a factor of 10:
      #define MAX_VALUES 100000
      Re-compile (type make), and then run the time command on binary and linear

    4. Try making MAX_VALUES bigger, re-compile, and re-time.