CSCI 362 — Lab 2a

Elementary Sorting Algorithms

Due: May 25, 2019 @ 11:59PM

Handout

Grab the handout tarfile from here: http://cs.millersville.edu/~wkillian/2019/summer/files/csci362/lab2a-sorts.tar

You can extract a tarfile with the following command:

tar xvf <filename.tar>

Overview

Implement optimized bubble sort, insertion sort, and selection sort, and compare their performance. Implement each as a separate function that takes a vector<int> by reference.

  1. Input the following:

    • the number of elements to sort
    • the sorting algorithm to use ('bubble', 'insertion', or 'selection')
    • the sequence type ('a' for ascending, 'd' for descending, and 'r' for random)
      • If the type is 'a', generate the sequence 1, 2, 3, …, N.
      • If the type is 'd', generate the sequence N, N - 1, N - 2, …, 1
      • If the type is 'r', use a std::minstd_rand random number engine
        • You will need an additional input (seed) to generate random int's between 0 and 9,999, inclusive, for the vector elements.
  2. After you populate the initial vector ('A'), use it to construct ONE additional vector named 'ACopy'.

  3. Sort 'A' with the algorithm the user specifies, and 'ACopy' with std::sort.

  4. Check that your sorting algorithm is correct by comparing 'A' with 'ACopy' using operator== for vector. If you do NOT do this, your sorting algorithms will be considered incorrect!

  5. To better understand the efficiency of each algorithm, count the number of KEY comparisons and swaps each sort performs. Count ONLY comparisons in which one key is compared to another key (e.g., A[i] < A[j]), and NOT comparisons to check if a subscript is in bounds.

ENSURE all of your sorts are correct. An incorrect sort will result in a MAJOR deduction.

AVOID global variables and USE standard C++ range conventions [ begin, end ).

Input Specification

  1. size of the vector ('N', of type size_t)
  2. the sorting algorithm ('algorithm', of type std::string: USE 'bubble', 'insertion', or 'selection')
  3. the sequence type ('type', of type char: USE 'a' for ascending, 'd' for descending, and 'r' for random).
    • If the sequence type is 'r', input a non-negative seed.

Use PRECISELY the format below with the EXACT same SPACING and SPELLING.

N         ==> <UserInput>
Algorithm ==> <UserInput>
Type      ==> <UserInput>
Seed      ==> <UserInput> // ONLY if Type is 'r'

Output Specification

Output the number of key comparisons, the number of swaps, and whether the algorithm correctly sorted the input vector, by comparing 'A' to the result produced by std::sort. If the vectors are the same, output 'yes', otherwise output 'no'.

Use PRECISELY the format below with the EXACT same SPACING and SPELLING.

Sample Output

N         ==> 1000
Algorithm ==> insertion
Type      ==> d

# Compares: 499500
# Swaps   : 0
Sort ok?    yes

Required Methods

Include at LEAST the following methods.

// Perform an optimized bubble sort on 'v'
// Update the number of comparisons and swaps performed
template <typename T>
void
bubbleSort (vector<T>& v, Statistics& stat);

// Perform an insertion sort on 'v'
// Update the number of comparisons performed
template <typename T>
size_t
insertionSort (vector<T>& v), Statistics& stat);

// Perform a selection sort on 'v'
// Update the number of comparisons and swaps performed
template <typename T>
void
selectionSort (vector<T>& v, Statistics& stat);

Hints

The sort routines should initialize the two member fields of Statistics 'numCompares' and 'numSwaps' to zero.

To correctly count the number of comparisons in 'insertionSort' you'll have to introduce an 'if' statement.

Use operator== to test if your sort results match those of std::sort. Do NOT use any other method.

Style

Use good programming style:

Submission

Please submit a tarball containing two files: driver.cpp and sort.hpp to autolab

You can generate this tarfile (called handin.tar) by invoking the following command:

make handin

Grading

This lab is graded out of 75 points