CISC4080, Computer Algorithms,
Lab 1

     

Home

Schedule

Assignments

Grading

 

Goal:

  1. Review simple sorting algorithms
  2. Practice testing (for correctness), measuring and comparing performance of different algorithms
  3. Familiarize with C++ STL class vector

Overview:

  1. Implement, test and compare the performance of sorting algorithms: bubble sort, selection sort, insertion sort by sorting a list (implemented using array or vector) of ints.
  2. (Extra credits*) Work on improve the code efficiency in both running time and memory requirement.

The following example illustrates how your program should work:

    [storm: zhang]$ a.out 
    1. Testing the three algorithms:
    Testing bubble sort with an array of size 10: correct! 
    Testing selection sort with an array of size 10: correct! 
    Testing inserion sort with an array of size 10: correct!
    2. Measuring the three sorting algorithms with 10 ranondomly generated inputs
    Measuring running time for array of size 20
    3. Summary of measurement result
    Input-size          bubble            selection             Insert
                    avg  min  max      avg   min   max       avg   min  max
    20              ... ...    ... 
    

Details:

Here is a step-by-step guide to finish this lab:

  1. You can use dynamic array or vector to store the list of ints to be sorted. The following instruction gives function prototype for both approaches.
  2. Implement helper functions for generating random list of ints, and checking if a list is sorted, and duplicate the list.
          /**************************************************************
           If you use array:
           ***************************************************************/
    	/* Generate an array of given size, fill it with random integers between 1 and 10,000
    	   return the address of the array 
    	   @param size: the size of the array to be generated
    	   @return the address of the array */
    	int * GenerateRandomArray(int size); 
    
    	/* display the content of the array */
    	void PrintArray (int * array, int size);
    
    	/* Duplicate the given array 
               Create an array that is a duplicate of the given orig array
    	   @param orig: address of original array
    	   @param size; size of original array
    	   @return: address of the newly created array */
    	int * DuplicateArray (int * orig, int size);
    
    	/* Test if an array is sorted in ascending order or not 
    	 @param array: address of array to be checked
    	 @param size: size of array to be checked
    	 @return: true if the array is sorted in the given mode; false otherwise */
    	bool IsSorted (int * array, int size);
    
    
          /**************************************************************
           If you use vector
           ***************************************************************/
         /* Generate an vector and fill it with random integers between 1 and 10,000
    	   return the address of the array
    	   @param size: the size of the array to be generated
    	   @return the vector */
    	vector<int> GenerateRandomVector(int size);
    
    	/* display the content of the vector */
    	void PrintVector (vector<int> & intList);
    
    	//Just use assignment operator to duplicate a vector 
    	/* NO NEED TO HAVE THIS ANYMORE  */
    	// int * DuplicateArray (int * orig, int size); 
    
    	/* Test if an array is sorted in ascending order or not
    	 @param array: address of array to be checked
    	 @param size: size of array to be checked
    	 @return: true if the array is sorted in the given mode; false otherwise */
    	bool IsSorted (vector<int> & intList);
    
    
    
    
        
  3. Implement and testing bubble sort function with the following prototype:
        /*****************************************************
         If you use array 
         ****************************************************/
        	/* sort the array of int in ascending order using bubble sort
    	@param array: address of the array 
    	@param size: size/length of the array
    	@postcondition: the elements in the array is arranged in ascending order
    	*/
    	void BubbleSort (int * array, int size); 
    
    
        /*****************************************************
         If you use vector
         ****************************************************/
        	/* sort the vector of int in ascending order using bubble sort
    	@param intList: vector of ints
    	@postcondition: the elements in the vector is arranged in ascending order
    	*/
    	void BubbleSort (vector<int> &  intList)
    
        
  4. Test bubble sort function as below:
    	   /*************************************
    	   If you use array
    	   **************************************/
               int * array = GenerateRandomArray (10);
    	   cout <<"Sorting array:";
    	   PrintArray (array, 10);
    	   BubbleSort (array,10);
    	   cout <<"After:";
    	   PrintArray (array, 10);
    	   if (IsSorted (array,10))
    	       cout <<"BubbleSort passed testing!\";
    	   else
    	       cout <<"BubbleSort does not work!\n";
    	   //deallocate dynamic memory 
       	   delete [] array; 
    	 
    	   /*************************************
    	   If you use vector
    	   **************************************/
               vector<int> intVector = GenerateRandomVector (10);
    	   cout <<"Sorting vector:";
    	   PrintVector (intVector);
    	   BubbleSort (intVector);
    	   cout <<"After:";
    	   PrintArray (intVector);
    	   if (IsSorted (intVector))
    	       cout <<"BubbleSort passed testing!\";
    	   else
    	       cout <<"BubbleSort does not work!\n";
    	   //when intVector goes out of scope, its memory will be 
    	   //deallocated by its destructor 
       
  5. Similarly, implement and testing the selection sort, insertion sort.

  6. Measuring and comparing performance: Measure the running time of the above sorting algorithms as the following pseudocode suggested: Here is an example code that illustrate how to measure the running time. Here is a link to online manual of gettimeofday libary function.
    /*
     Measure running time of three sorting algorithms, using
     randomly generated array of given size (input_size)
     Find and return avg, min and max running time for the @run_numer
       random runs 
    */
    void MeasureSortingFunction (int input_size, int run_number)
    {
       ...
    
       Repeat for run_number of times 
           same_input = Generate a random array of integers of input_size 
        
           create work_copy which is a copy of same_input
           Sort work_copy using bubble sort 
           update min, max and sum of bubble sort algorithms 
     
           create work_copy which is a copy of same_input
           Sort work_copy using selection sort 
           update min, max and sum of selection sort algorithms 
     
           create work_copy which is a copy of same_input
           Sort work_copy using insertion sort 
           update min, max and sum of insertion sort algorithms 
            
       calculate average, display avg, min and max for each sorting algorithms
    
  7. (Extra Credits): Repeat the measurement of the last step for different input size (lenght of the array or vector) varying from 10, 20, 40, 50, 100, 200, 500, 1000. Do the running time of the three algorithms grow quadratically when the size of input grows? How do you explain the difference in running time by the three algorithms? Answer these questions in your code as comments.

What to submit

Please submit all your source codes (.cc, .cpp, .h) one by one. For example, if you have two .cpp files (lab1_main.cpp and sorting.cpp), do the following:

	submit4080 LAB1 lab1_main.cpp
	submit4080 LAB1 sorting.cpp