Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SortingVisualizer

A Unity app that visualizes sorting algorithms as a row of 3D bars. It cycles through each algorithm in turn, showing the name of the current sort along with running comparison and swap counts and elapsed time.

Built with Unity 2019.2.8f1.

Written in 2020. Archived to GitHub in 2026.

Quick Sort partway through a run, with bars being compared shown in cyan and bars being swapped in red

How it works

The algorithms don't animate anything directly. Each one sorts a plain int[] to completion first and records what it did, and the scene replays that recording afterwards.

A SortingInterface subclass sorts the array, calling CompareGT, CompareLT and AddSwap as it goes. Each of those appends a SortingStatsEntry holding the operation type, the indices involved, the resulting values, and a Stopwatch reading in milliseconds. Once the sort finishes, SortManager walks that list one entry per frame, recoloring the bars involved and applying any swapped values.

Splitting it this way keeps each sort readable as ordinary code, with no coroutines or yields mixed into the logic. It also means the reported timings measure the sort itself rather than the frame rate it gets drawn at.

During playback a comparison flashes the bars cyan and a swap flashes them red, after which they fade back to the default color. When the array is fully sorted it sweeps green before the next algorithm starts.

Display

Bars are cube instances whose Y scale comes from their value (see Element.SetValue), laid out along the X axis. CameraHelper frames the whole array by encapsulating the bounds of every child renderer, so the view adjusts to whatever element count is set.

Algorithms

Quick, Bubble, Bubble (optimized), Shell, Insertion, Selection, Sleep, and Bogo.

SleepSort is a working implementation of the joke algorithm. It starts one coroutine per element, each waiting a number of frames proportional to its value before writing itself back to the array.

Bogo Sort is implemented and can be selected, but the auto-cycling queue skips it, since it would stall the demo indefinitely.

Starting arrangements

These use the same interface as the sorts, so the scramble is animated too.

Mode Effect
Shuffle Randomly permutes an already-ordered array
InverseRamp Reverses the array, which is the worst case for most of these sorts
Randomize Assigns each slot a fresh random value

Controls

  • Space: skip to the next algorithm
  • Dropdown: choose the starting arrangement, which restarts the current sort

Configuration

On the SortManager component:

Field Default Meaning
m_ElementCount 100 Number of bars
m_MaxHeight 10 World-space height of the largest bar
m_ValuesType LinearShuffled Starting arrangement
m_DefaultElementColor n/a Color bars fade back to after a highlight

About

Unity visualizer for sorting algorithms: records every comparison and swap, then replays them as animated 3D bars with live stats

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages