A C++ console program that implements and compares bubble sort, selection sort, insertion sort, and quicksort on the same randomly generated dataset.
Algorithm descriptions can feel abstract until their runtime differences are measured on identical input. This project makes those tradeoffs observable.
The program:
- generates 50,000 random integers;
- copies the original values before every benchmark;
- times each algorithm with
std::chrono; - prints the results to the console; and
- writes the same results to
output.txt.
- C++17
- Standard library timing and file streams
- Visual Studio-compatible source files
g++ -std=c++17 -O2 main.cpp sort.cpp -o sorting-benchmark
./sorting-benchmarkOne run on 50,000 integers produced:
| Algorithm | Time |
|---|---|
| Bubble sort | 7,390 ms |
| Selection sort | 4,268 ms |
| Insertion sort | 743 ms |
| Quicksort | 6 ms |
Exact times vary by machine and random input. The important result is the scale of the difference between the quadratic algorithms and quicksort.
- Ensured every algorithm receives the same unsorted input.
- Used static arrays to avoid stack pressure at the selected data size.
- Separated the algorithms into a header and implementation file.
- Recorded reproducible evidence in a human-readable output file.
Implementation details and input distribution matter, but algorithmic complexity dominates as the dataset grows. Benchmark design also matters: comparing different input arrays would make the results less trustworthy.
main.cpp— dataset creation, timing, outputsort.cpp— four algorithm implementationssort.h— public function declarationsoutput.txt— sample benchmark run
