CISC4080, Computer Algorithms,
Lab 2

     

Home

Schedule

Assignments

Grading

 

Goal:

  1. Understand the working of merge sort and quicksort.
  2. Appreciation of implementation and its impact on algorithm efficiency: in particular, recursive v.s. iteraitve implementation of mergesort
  3. Understand the working and benefit of randomized algorithms
  4. Please pay attention to the grading critera!

Overview:

  1. Implement, test and compare the performance of sorting algorithms: merge sort (recursive and iterative implementation), and quicksort.
  2. 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!
    Testing merge sort (recursive) with an array of size 10: correct!
    Testing merge sort (iterative)with an array of size 10: correct!
    Testing quick 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, 40, 50, 100, ... 1000
    3. Summary of measurement result
    n    bubble selection  Insert  Merge(1) Merge(2) QuickSort 
         m M a  m  M  a    m M a   m M a    m M a    m M  a 
    20  ... ...    ... 
    40
    50
    100
    200
    500
    1000
    

Details:

Use your lab1 code as starting point for this lab, and follow the following step-by-step guide to finish this lab:

  1. Implement mergesort algorithm using a recursive function, based upon the pseudocode studied in class.
  2. Implement mergesort algorithm without recursive calls. Please add comment in your code to explain (to be best of your knowledge) how many times each element is copied during the mergesort implementation. Here are two different ways to do this (as explained in class):
    • Crafting a nested loop (two-level) to merge sublists of size 1 to sublist of size 2, then merge sublists of size 2 to size 4, and so on.
    • Using a C++ STL queue to stores the sublists to be merged (as given in MergeSort.cpp)
  3. Implement the randomized quicksort algorithm, i.e., in the partition function below, first randomly select a pivot point, exchange it with the last element, and then continue on partitioning the array:
    //Please include stdlib.h so that you can use random() 
    //A is the array to be partitioned, p and r are index of the left and right end of the array
    //return the location (index) of pivot value after partition
    // Post-condition: A[p...return_value-1] < A[return] <= A[return+1...r]
    int Partition (A[p...r])
    {
         //note that the size of the array to be partitioned is: r-p+1 
         c = random()%(r-p+1)+p;   //% operation reduce the random number to the range of 0, ... r-p
         				// +p to shift the value to p,p+1, ...r
         exchange A[c] with A[r]
    
         //Below is copied from parittion pseudocode given in slides
         x = A[r]
         i = p-1
         for j=p to r-1
         	....
         ... 
    
    }
    	

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 LAB2 lab2_main.cpp