Due: April 20th
Analysis of algorithms concerns two broad concepts: correctness and complexity. Given that one or more algorithms all produce the correct output for any given input, we are interested in the resources they each require. In this lab, you will implement three sorting algorithms in C++ and analyze their performance.
- Starter Code: Create a branch or a fork of the provided starter code repository.
- Implementation: Complete the logic in
algorithms.cc. - Public URL: Submit the public URL of your GitHub repository (specifically the branch containing your work) to [LMS Link].
- Note: Ensure your repository is set to Public so the grading scripts can access your code.
You must implement the following three algorithms in C++ exactly as described in the provided algorithms.cc skeleton.
- let B be an empty sequence
- while A is not empty do:
- let x be min{A}
- let A be A − (x) and B be B ++ (x)
- return B
- let mid be
$\lfloor n/2 \rfloor$ - if n = 1 then return A
- else return Merge(MergeSort(left_half), MergeSort(right_half))
- let T be an empty frequency array, B be an empty sequence
- for i from min{A} to max{A}: T(i) = 0
- for all a in A: T(a) = T(a) + 1
- for i from min{A} to max{A}:
- for j from 1 to T(i): B = B ++ (i)
- return B
Before analyzing performance, ensure your algorithms actually work.
- Command:
g++ -O3 algorithms.cc test.cc -o unit_test && ./unit_test
The code to generate timing data is provided for you. This script will test sequences from size 100 to 10,000 using random integers.
- Command:
g++ -O3 algorithms.cc analyzer.cc -o analyze && ./analyze > results.csv
The generated results.csv will follow this format:
Size,Selection,Merge,Counting
100,0.011722,0.039685,6.86176
200,0.044202,0.071844,5.28226
300,0.079629,0.110727,5.245
...You are required to submit a report.pdf. You may use any data visualizer you prefer (Microsoft Excel, Google Sheets, Python/Matplotlib, etc.) to process the results.csv file.
The report must include:
-
Scatter Plot: A single plot showing the running time of all three algorithms. The horizontal axis (
$x$ ) should be the sequence size, and the vertical axis ($y$ ) should be the running time in milliseconds. - Comparison: You should have 300 data points total (100 per algorithm).
-
Brief Analysis: A short paragraph explaining why each curve matches its theoretical complexity (
$O(n^2)$ ,$O(n \log n)$ , or $O(n+k)$).
- Iterators: Functions like
std::min_elementreturn an iterator. Use*itto get the value andA.erase(it)to remove the element. - Frequency Array: In CountingSort, remember that the index for a value
xisx - min_value. - Slicing: Use the vector constructor for MergeSort:
std::vector<int> left(A.begin(), A.begin() + mid); - Optimization: Always include the
-O3flag when compiling to ensure the hardware is running your code efficiently!