Skip to content
Laviyx Learn

Try

Mini project

Sorting Algorithm Visualizer

A web app that animates bubble, selection, insertion, merge, quick and heap sort as moving bars, counting comparisons and swaps so you can see why some algorithms are faster.

Intermediate 2 weeks 1–2 studentsUpdated

HTMLCSSJavaScript

The problem

Big-O notation is easier to understand when you can watch it. On paper, O(n²) and O(n log n) look like symbols; on screen, you can see bubble sort crawl while merge sort finishes in a few passes. This visualizer turns each sorting algorithm into an animation with live counts.

Features to build

  • Generate a random array of bars, with a slider for the array size
  • Animate at least six algorithms: bubble, selection, insertion, merge, quick and heap sort
  • Colour bars being compared, swapped and already in place
  • Speed slider, plus pause, resume and step-by-step buttons
  • Live counters for comparisons and swaps or writes
  • Show each algorithm's best, average and worst time complexity and its space use
  • Start states for testing: random, nearly sorted, reversed and many duplicates

Build plan

  1. 1

    Separate sorting from drawing

    Run each algorithm on a copy of the array and record a list of steps such as compare(i, j), swap(i, j) or set(i, value). The algorithms stay simple and testable.

  2. 2

    Animate the steps

    Play the recorded steps one at a time with a delay from the speed slider, updating bar heights and colours. Pause and step just move through the list.

  3. 3

    Count the work

    Increase the counters as each compare or swap step plays, so the numbers match what is on screen.

  4. 4

    Compare inputs

    Run each algorithm on random, nearly sorted and reversed data and note how the counts change, for example insertion sort on nearly sorted input.

  5. 5

    Test correctness

    After each algorithm, check that the result equals the array sorted with the built-in sort, for many random arrays.

Learn these first

Each part of the project is taught in one of these free lessons.

Make it stand out

  • Race two algorithms side by side on the same array
  • Plot comparisons against array size for each algorithm
  • Add counting sort and radix sort for integer keys
  • Show the code of the running algorithm with the current line highlighted

Viva and interview questions

Try to answer each one aloud before you open it.

  • What does it mean for a sort to be stable?

    Equal items keep their original order. Merge sort and insertion sort are stable; quick sort and heap sort usually are not. Stability matters when sorting records by one field after another, like by name and then by marks.

  • When is quick sort slow?

    When the pivot splits the array very unevenly every time, for example picking the first element as pivot on already sorted data. That makes it O(n²). Picking a random pivot or the median of three makes this very unlikely.

  • Why is insertion sort fast on nearly sorted data?

    Each element only moves a short distance to its place, so the work is close to one pass through the array: O(n) in the best case. That is why many library sorts use insertion sort for small or nearly sorted parts.

  • Why record steps instead of animating inside the algorithm?

    It keeps the algorithm code clean and correct, makes it easy to test, and lets the player pause, step or change speed without changing the algorithm at all.

  • What extra space does merge sort need?

    The usual array version needs O(n) extra space for merging. Heap sort sorts in place with O(1) extra space, and quick sort needs O(log n) on average for its recursion.

What to put in your project report

  1. Abstract: the problem, your solution and the result in one paragraph.
  2. Introduction and problem statement: who has the problem and why it matters.
  3. Requirements: the features (functional) and qualities such as speed and security (non-functional).
  4. Design: architecture diagram, database tables or data flow, and screen layouts.
  5. Implementation: the main modules and the most interesting code, explained.
  6. Testing: the test cases you ran, with inputs, expected and actual results.
  7. Results: screenshots or numbers that show the project working.
  8. Conclusion and future scope: what you achieved and what you would add next.

Your college may give its own format. Follow that first, and use this list to check nothing is missing.

All project ideas →