Insertion Sort Visualizer

Unsorted
Sorted
Current
Comparing
Shifting
Inserting
0
Elements
0
Pass
0
Comparisons
0
Shifts
Execution

About Insertion Sort Visualizer

The Insertion Sort Visualizer helps you understand how insertion sort works—step by step, in real time. Instead of just reading theory, you can see each comparison, shift, and insertion as the algorithm sorts the array.

What You Can Do

  • Enter your own numbers or generate random data
  • Run the algorithm automatically or step through each action
  • Adjust speed to learn at your own pace
  • Track comparisons, shifts, and passes in real time

Why Use It?

Insertion sort can be tricky to visualize from code alone. This tool makes it simple by showing:

  • How elements move within the sorted portion
  • Where each value gets inserted
  • How the sorted section grows step by step

How to Use the Insertion Sort Visualizer

  1. Enter Data
    • Type numbers separated by commas, or click Random to generate values.
  2. Load Array
    • Click Load to display the bars.
  3. Start Sorting
    • Run → auto sort
    • Step → move one step at a time
  4. Control Speed
    • Adjust the Speed slider to slow down or speed up the animation.
  5. Watch the Process
    • Colors show comparing, shifting, and inserting
    • The log explains each step clearly

How Insertion Sort Works

Insertion sort is a simple sorting algorithm that builds a sorted list one element at a time by inserting each value into its correct position. It works like arranging books on a bookshelf by inserting each book into its correct position among already arranged books.

Step-by-Step Process

  1. Start with the second element (the first element is already considered sorted)
  2. Compare the current element with the one before it
  3. If it’s smaller, shift the larger element to the right
  4. Continue comparing and shifting until the correct position is found
  5. Insert the element in its correct place
  6. Repeat until the entire array is sorted

Example

Array: 23, 11, 35, 8

  • Pick 11, compare with 23 → shift 23 → insert 11 → [11, 23, 35, 8]
  • Pick 35, already in correct place → [11, 23, 35, 8]
  • Pick 8, shift 35, 23, 11 → insert 8 → [8, 11, 23, 35]

Time Complexity

  • Best Case: O(n) (already sorted)
  • Worst Case: O(n²) (reverse order)