Skip to content

Insertion Sort With Every Shift
Step by Step

Insertion sort keeps the left part sorted. It lifts out the next number, shifts bigger ones right and drops the number into the gap.

Step through every shift in both directions, then share a link to the step where the gap opened.

What You Watch While the Sorted Part Grows

The lifted number, the shifts and the sorted part change on every step.

Insertion sort treats the left part as sorted and inserts one number at a time.

CodeStory's visualiser shows every comparison and shift, pass by pass. Marked code lines follow every move, and you can step back, step forward or play.

Try a nearly sorted list to see few shifts. Then try a reversed list to see the most shifts.

How Insertion Sort Fills the Gap

Steps below follow the sorted part on your screen.

  1. 1Lift out the next number after the sorted part.
  2. 2Compare it with the sorted numbers from right to left.
  3. 3Shift every bigger number one place right.
  4. 4Drop the lifted number into the gap and repeat.

Insertion Sort Cost in Time and Space

Nearly sorted lists need few shifts, while reversed lists need the most.

Time
O(n²)counted from the comparisons and shifts you step through
Space
O(1)numbers shift inside the list, so extra memory stays constant
Pattern
SortingOrder grows one comparison at a time.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Insertion Sort Visualiser FAQs

Open the visualiser and follow the sorted part as it grows.

Open the visualiser
What does the insertion sort visualiser show?

CodeStory's visualiser shows every lifted number, shift and insert, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of insertion sort?

O(n²) time and O(1) space. Live counts show the comparisons and shifts growing with the list.

When is insertion sort faster than other sorts?

Insertion sort needs few shifts on a nearly sorted list. Compare a nearly sorted list and a reversed list in the visualiser to see the gap.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Sorting? Get a Free Callback

Tell CodeStory which sorting step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from sorting basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.