Capstone Project 3

Sorting Algorithm Visualizer

Build an interactive console-based sorting algorithm visualizer in C. You will implement multiple sorting algorithms (Bubble, Selection, Insertion, Merge, Quick, Heap), create animated step-by-step visualizations using ASCII art, measure and compare performance metrics, and build a menu-driven interface for algorithm selection.

10-15 hours
Intermediate
400 Points
What You Will Build
  • 6 sorting algorithm implementations
  • ASCII-based visual animation
  • Performance comparison tools
  • Step-by-step execution mode
  • Interactive menu-driven CLI
Contents
01

Project Overview

This capstone project focuses on algorithm implementation and visualization. You will work with test datasets containing arrays of various sizes (100 to 10,000 elements) with different initial orderings (random, sorted, reverse-sorted, nearly-sorted). Your goal is to build a professional sorting visualizer that animates each algorithm step-by-step in the console, tracks comparisons and swaps, measures execution time, and allows users to compare algorithm performance.

Skills Applied: This project tests your proficiency in algorithms, pointers, arrays, function pointers, time measurement, console manipulation, and modular C programming with proper header files and separate compilation.
Algorithms

Implement 6 classic sorting algorithms with analysis

Visualization

ASCII-based animation showing each step

Performance

Track comparisons, swaps, and execution time

CLI Interface

Menu-driven command-line user interface

Learning Objectives

Technical Skills
  • Implement and understand sorting algorithm complexity
  • Use function pointers for algorithm selection
  • Measure execution time with clock() and time()
  • Manipulate console output for animation effects
  • Build modular programs with header files
Professional Skills
  • Analyze algorithm performance empirically
  • Create intuitive visual representations
  • Build user-friendly command-line interfaces
  • Write maintainable and well-documented code
  • Compare theoretical vs actual performance
Ready to submit? Already completed the project? Submit your work now!
Submit Now
02

Project Scenario

CodeAcademy Learning Platform

You have been hired as a Software Developer at CodeAcademy, an online learning platform that teaches programming to beginners. The curriculum team wants to add an interactive tool that helps students understand how different sorting algorithms work by visualizing each step of the sorting process.

"Students often struggle to understand sorting algorithms from just reading pseudocode. We need a visual tool that shows exactly how Bubble Sort, Quick Sort, and others rearrange elements step by step. Can you build a console-based visualizer that makes algorithms come alive?"

Prof. James Chen, Computer Science Curriculum Lead

System Requirements from the Client

Algorithm Library
  • Bubble Sort with optimization flag
  • Selection Sort
  • Insertion Sort
  • Merge Sort (recursive)
  • Quick Sort (with pivot selection)
  • Heap Sort
Visualization Features
  • Bar chart display using ASCII characters
  • Highlight current elements being compared
  • Show swap operations with color
  • Adjustable animation speed
  • Step-by-step or continuous mode
Performance Metrics
  • Count total comparisons
  • Count total swaps/moves
  • Measure execution time (milliseconds)
  • Display complexity notation
  • Compare multiple algorithms side-by-side
Data Generation
  • Generate random arrays of any size
  • Load arrays from test files
  • Create sorted, reverse, nearly-sorted data
  • Allow custom array input
  • Save results to file
Pro Tip: Use function pointers to create a generic sorting interface. This allows you to pass any sorting algorithm to your visualizer and benchmark functions without duplicating code.
03

The Dataset

You will work with random number arrays specifically designed for sorting algorithm testing and performance analysis. The Kaggle dataset contains CSV files with different sizes to benchmark algorithm performance at various scales:

Sample Dataset Download

Download random number files for testing your sorting implementations (same format as Kaggle).

Random Numbers CSV Files:
Random floating-point numbers (0-1) - identical format to Kaggle dataset
Full Dataset from Kaggle

This project uses the Random Numbers dataset from Kaggle - CSV files with random numbers generated using np.random.rand() for algorithm benchmarking:

CSV Files Available:
  • random_numbers_1000.csv - 1,000 numbers
  • random_numbers_10000.csv - 10,000 numbers
  • random_numbers_100000.csv - 100,000 numbers
  • random_numbers_1000000.csv - 1 million numbers
  • random_numbers_10000000.csv - 10 million numbers
Full Dataset: 5 CSV files | Sizes: 1,000 to 10,000,000 elements | Random floating-point numbers (0-1) generated with numpy | CC0 Public Domain License
Dataset Schema

FileRecordsUse Case
random_numbers_1000.csv1,000 numbersQuick testing
random_numbers_10000.csv10,000 numbersSmall benchmarks
random_numbers_100000.csv100,000 numbersMedium benchmarks
random_numbers_1000000.csv1,000,000 numbersLarge benchmarks
random_numbers_10000000.csv10,000,000 numbersStress testing
Array Sizes Available: 1,000 10,000 100,000 1,000,000 10,000,000
Usage Tip: Each CSV file contains random floating-point numbers (0-1). Read with fscanf(file, "%lf", &arr[i]) for doubles. Compare algorithm performance across different array sizes to analyze time complexity.
Local Samples: 5 CSV files (1K to 10M)
Full Kaggle: 5 CSV files (1K to 10M)
Perfect For: Algorithm benchmarking
04

Key Concepts

Before diving into the implementation, understand these fundamental concepts that power your sorting visualizer. These concepts are essential for building an efficient and educational algorithm visualization tool.

Sorting Algorithms
  • Bubble Sort: O(n^2) - Compare adjacent pairs, swap if out of order
  • Selection Sort: O(n^2) - Find minimum, place at beginning
  • Insertion Sort: O(n^2) - Build sorted portion one element at a time
  • Merge Sort: O(n log n) - Divide, sort halves, merge back
  • Quick Sort: O(n log n) avg - Partition around pivot recursively
  • Heap Sort: O(n log n) - Build max-heap, extract maximum
Visualization Techniques
  • Bar Chart: Use ASCII characters to draw bars proportional to values
  • Color Coding: ANSI escape codes for highlighting comparisons/swaps
  • Animation: Clear screen and redraw to create motion effect
  • Speed Control: Use usleep() or Sleep() to control delay
  • Step Mode: Wait for keypress between each operation
  • Legend Display: Show current operation and metrics
Performance Metrics
  • Comparisons: Count each time two elements are compared
  • Swaps/Moves: Count each time elements change position
  • Time: Use clock() to measure CPU time in milliseconds
  • Memory: Track auxiliary space for algorithms like Merge Sort
  • Iterations: Count outer loop passes for iterative algorithms
  • Recursion Depth: Track maximum stack depth for recursive algorithms
Function Pointers
  • Sort Function Type: Define typedef for sort function signature
  • Algorithm Array: Store all algorithms in function pointer array
  • Generic Visualizer: Pass algorithm as parameter to visualize()
  • Benchmark Runner: Loop through all algorithms using pointers
  • Callback System: Notify visualizer on each comparison/swap
  • Clean Interface: Single function handles all algorithm types
Project Structure
include/
  • sorting.h - Algorithm declarations
  • visualizer.h - Display functions
  • metrics.h - Performance tracking
  • utils.h - Helper functions
  • menu.h - Menu interface
src/
  • bubble.c - Bubble sort
  • selection.c - Selection sort
  • insertion.c - Insertion sort
  • merge.c - Merge sort
  • quick.c - Quick sort
  • heap.c - Heap sort
  • visualizer.c - Display logic
  • metrics.c - Performance tracking
  • menu.c - User interface
  • main.c - Entry point
Other Files
  • data/ - Test arrays
  • tests/ - Unit test files
  • Makefile - Build config
  • README.md - Documentation
Your Task: Implement all sorting algorithms with visualization callbacks. Start with simple algorithms (Bubble, Selection), then add visualization, then implement advanced algorithms (Merge, Quick, Heap), and finally build the comparison tools.
05

Project Requirements

Complete all components below to build a fully functional sorting visualizer. Each step builds upon the previous one, taking you from basic algorithms to an interactive educational tool.

1
Basic Sorting Algorithms

Implement O(n^2) Algorithms:

  • Bubble Sort with early termination optimization
  • Selection Sort finding minimum each pass
  • Insertion Sort building sorted portion
  • Each algorithm counts comparisons and swaps
Deliverable: bubble.c, selection.c, insertion.c with all three algorithms correctly sorting arrays and returning metrics.
2
Advanced Sorting Algorithms

Implement O(n log n) Algorithms:

  • Merge Sort with auxiliary array allocation
  • Quick Sort with median-of-three pivot selection
  • Heap Sort with heapify operations
  • Track recursion depth for recursive algorithms
Deliverable: merge.c, quick.c, heap.c with efficient implementations and proper memory management.
3
Visualization Engine

ASCII Bar Chart Display (visualizer.c):

  • Draw horizontal or vertical bars for each element
  • Highlight elements being compared (different color)
  • Highlight elements being swapped (another color)
  • Show current operation description
  • Display running metrics (comparisons, swaps, time)
Deliverable: visualizer.c with draw_array(), highlight_compare(), highlight_swap(), and update_display() functions.
4
Performance Metrics

Metrics Tracking (metrics.c):

  • Metrics structure with comparisons, swaps, time, memory
  • Timer functions using clock() for CPU time
  • Reset and accumulate functions for benchmarking
  • Format and print metrics in table format
Deliverable: metrics.c with complete performance tracking and formatted output for algorithm comparison.
5
Menu Interface

Main Menu (menu.c):

  • Select sorting algorithm from list
  • Choose data source (file, random, custom)
  • Set visualization speed (fast, medium, slow, step)
  • Run single algorithm or compare all
  • View benchmark results table
Deliverable: Polished menu.c with complete menu system, all operations working, input validation, and user-friendly prompts.
6
Benchmark Comparison

Algorithm Comparison:

  • Run all algorithms on same input data
  • Display side-by-side metrics table
  • Rank algorithms by each metric
  • Test with different input patterns
  • Export results to CSV file
Deliverable: Benchmark function that runs comprehensive comparison and produces formatted output and optional CSV export.
Development Tip: Implement and test each algorithm without visualization first. Once sorting works correctly, add the visualization callbacks. This makes debugging much easier.
06

Example Usage

Your sorting visualizer should provide a clear, menu-driven interface. Below are the expected features and sample workflows for each major operation.

Main Menu Options
  1. Visualize Sorting Algorithm
  2. Run Single Algorithm (no visual)
  3. Compare All Algorithms
  4. Generate Random Array
  5. Load Array from File
  6. Enter Custom Array
  7. Set Visualization Speed
  8. View Algorithm Info
  9. Export Results to CSV
  10. Exit
Algorithm Selection

Choose from 6 sorting algorithms:

  • Bubble Sort - Simple, O(n^2), stable
  • Selection Sort - Simple, O(n^2), unstable
  • Insertion Sort - Adaptive, O(n^2), stable
  • Merge Sort - Divide and conquer, O(n log n), stable
  • Quick Sort - Partition-based, O(n log n) avg, unstable
  • Heap Sort - Heap-based, O(n log n), unstable
Visualization Display
  • Bar Chart: ASCII bars showing element values
  • Compare Highlight: Yellow for elements being compared
  • Swap Highlight: Green for elements being swapped
  • Sorted Highlight: Cyan for elements in final position
  • Speed Control: Instant, Fast (10ms), Medium (50ms), Slow (200ms), Step
Benchmark Output
  • Algorithm Name: Name of sorting algorithm
  • Comparisons: Total number of comparisons
  • Swaps: Total number of swaps/moves
  • Time (ms): Execution time in milliseconds
  • Complexity: Time and space complexity notation
Test Cases for Validation
Test CaseInputExpected Result
Small Random10 random elementsAll algorithms sort correctly
Already Sorted1,2,3,4,5,6,7,8,9,10Bubble/Insertion detect early, minimal work
Reverse Sorted10,9,8,7,6,5,4,3,2,1Maximum comparisons for Bubble/Selection
Duplicates5,3,8,3,9,3,1,3All algorithms handle duplicates correctly
Single Element42Return immediately, no operations
Empty Array(empty)Handle gracefully, no crash
Large Random1000 random elementsO(n^2) noticeably slower than O(n log n)
Negative Numbers-5,3,-8,0,9,-1Sort correctly including negatives
07

Submission Requirements

Create a public GitHub repository with the exact name shown below:

Required Repository Name
c-sorting-visualizer
github.com/<your-username>/c-sorting-visualizer
Required Project Structure
include/
  • sorting.h
  • visualizer.h
  • metrics.h
  • utils.h
  • menu.h
src/
  • bubble.c, selection.c, insertion.c
  • merge.c, quick.c, heap.c
  • visualizer.c
  • metrics.c
  • menu.c, main.c
Other
  • data/ (test arrays)
  • tests/ (test files)
  • Makefile
  • README.md
README.md Required Sections
1. Project Header
  • Project title and description
  • Your full name and submission date
  • Course and project number
2. Features
  • List of all sorting algorithms
  • Visualization capabilities
  • Performance metrics tracked
3. Build Instructions
  • Prerequisites (GCC, make)
  • How to compile with make
  • How to run the program
4. Usage Examples
  • Menu navigation guide
  • Sample visualization output
  • Screenshots or GIFs of animation
5. Algorithm Details
  • Brief description of each algorithm
  • Time and space complexity
  • Best/worst case scenarios
6. Benchmark Results
  • Performance comparison table
  • Analysis of results
  • Observations about complexity
7. Challenges and Solutions
  • Difficulties encountered
  • How you solved them
  • What you learned
8. Contact
  • GitHub profile link
  • LinkedIn (optional)
  • Email (optional)
Do Include
  • All source files (.c) and headers (.h)
  • Working Makefile with all targets
  • Test data files for validation
  • Test file with unit tests
  • Screenshots/GIFs showing visualization
  • Complete README with all sections
Do Not Include
  • Compiled binaries or object files (*.o, *.exe)
  • IDE-specific files (.vscode/, .idea/)
  • Temporary or backup files
  • Plagiarized code from online sources
Important: Before submitting, verify that your code compiles without warnings using gcc -Wall -Wextra and all sorting algorithms produce correctly sorted output.
Submit Your Project

Enter your GitHub username - we will verify your repository automatically

08

Grading Rubric

Your project will be graded on the following criteria. Total: 400 points.

Criteria Points Description
Basic Algorithms 60 Bubble, Selection, Insertion sort correctly implemented
Advanced Algorithms 90 Merge, Quick, Heap sort correctly implemented
Visualization 80 Clear ASCII display with highlighting and animation
Performance Metrics 50 Accurate tracking of comparisons, swaps, time
User Interface 40 Menu system, input validation, user-friendly messages
Benchmark Comparison 40 Side-by-side algorithm comparison with analysis
Code Quality 20 Modular design, comments, function pointers
Documentation 20 README quality, screenshots, complexity analysis
Total 400
Grading Levels
Excellent
360-400

Exceeds all requirements with exceptional quality

Good
300-359

Meets all requirements with good quality

Satisfactory
240-299

Meets minimum requirements

Needs Work
< 240

Missing key requirements

Ready to Submit?

Make sure you have completed all requirements and reviewed the grading rubric above.

Submit Your Project
09

Pre-Submission Checklist

Use this checklist to verify you have completed all requirements before submitting your project.

Sorting Algorithms
Visualization
Metrics and Comparison
Repository Requirements
Final Check: Run your visualizer with each algorithm on the provided test files. Verify the sorted output is correct and the visualization animates smoothly.
10

Common Issues and Solutions

Encountering problems? Here are the most common issues students face and how to resolve them quickly.

Quick Sort Stack Overflow
Problem

Program crashes on large or already-sorted arrays

Solution

Use median-of-three pivot selection instead of always picking first or last element. This prevents worst-case O(n^2) behavior on sorted input.

Screen Flicker During Animation
Problem

Visualization flickers annoyingly during animation

Solution

Use ANSI escape codes to move cursor to top of screen instead of clearing. Use printf("\033[H") to move to home position.

Merge Sort Memory Leak
Problem

Memory usage grows with each sort operation

Solution

Allocate a single auxiliary array once before sorting and pass it to the recursive function. Free it after sorting completes.

Inaccurate Time Measurement
Problem

Time shows 0ms for small arrays

Solution

Use clock() instead of time() for CPU time measurement. Calculate: (double)(end - start) / CLOCKS_PER_SEC * 1000.0 for milliseconds.

Still Having Issues?

Check the course discussion forum or reach out for help