DSA Lab
Insertion sort visualizer
Keep the left part sorted. Take the next item (the key), shift bigger items one place right, and drop the key into the gap. Small or nearly sorted lists, where it beats the O(n log n) sorts. Python's own sort uses it for short runs.
Insertion sort: its cost, and how to use this tool
Time: best O(n), average O(n²), worst O(n²). Extra memory: O(1). Stable, in place.
Sort a ready-made input or your own numbers. The bars are the array; the code beside them (in Python, C, C++ or Java) marks the line that just ran, and every step is explained and worked out below the bars.
Lessons that use it
- Bubble, selection and insertion sort (Algorithms)Three O(n²) sorts, step by step: swapping neighbours, picking the minimum, and inserting into a sorted part.
- Sorting algorithms (Python)Watch bubble sort and selection sort move values, then sort anything with sorted() and a key.