Skip to content

Latest commit

 

History

History

README.md

Sorting Algorithms Visualizer

Watch classic sorting algorithms sort the same 100-element array — step by step.

Python NumPy Matplotlib

Each bar is a value. Every frame is one swap or shift.
Same dataset, different strategies — see how efficiency really feels.


Quick Start

pip install -r requirements.txt
python BubbleSort.py    # pick any algorithm file below

On first run, a shared dataset file is generated so every algorithm sorts the exact same input.


Algorithms

Algorithm Best Average Worst In-place Stable
Bubble Sort O(n) O(n²) O(n²) Yes Yes
Selection Sort O(n²) O(n²) O(n²) Yes No
Insertion Sort O(n) O(n²) O(n²) Yes Yes
Merge Sort O(n log n) O(n log n) O(n log n) No Yes
Quick Sort O(n log n) O(n log n) O(n²) Yes No
Heap Sort O(n log n) O(n log n) O(n log n) Yes No
Counting Sort O(n + k) O(n + k) O(n + k) No Yes

Visualizations

All demos below use the same 100-element array.
Run HeapSort.py or CountingSort.py locally for the remaining two.

Bubble Sort

Repeatedly swaps adjacent out-of-order pairs — simple, but slow.

Bubble Sort


Insertion Sort

Builds a sorted prefix one element at a time — great on nearly-sorted data.

Insertion Sort


Selection Sort

Finds the minimum and places it — few writes, many comparisons.

Selection Sort


Merge Sort

Divide, conquer, merge — consistent O(n log n) performance.

Merge Sort


Quick Sort

Partition around a pivot — fast in practice, elegant recursion.

Quick Sort


Heap Sort & Counting Sort

File Idea
HeapSort.py Max-heap extraction — guaranteed O(n log n), no extra array
CountingSort.py Count occurrences — linear time when values are in a small range
python HeapSort.py
python CountingSort.py

How It Works

Every sorter is a Python generator that yields the array after each step. Matplotlib's FuncAnimation turns those frames into a live bar chart:

def bubbleSort(array):
    for i in range(len(array) - 1):
        for j in range(i + 1, len(array)):
            if array[i] > array[j]:
                array[i], array[j] = array[j], array[i]
                yield array   # ← one animation frame

To regenerate the shared dataset, delete the dataset file and run any script again.


Part of Cool Programming Challenges — a playground of small projects across languages.