Skip to content

Bubble Sort With Every Swap
Step by Step

Bubble sort swaps neighbours that are out of order. Every pass carries the largest remaining number to the end, and a pass without swaps means the list is sorted.

Step through every comparison and swap in both directions, then share a link to the pass that confused you.

What You Watch While Passes Sweep the List

Comparisons, swaps and the sorted tail change on every step.

Bubble sort compares neighbours and swaps them when they sit in the wrong order. After the first pass, the largest number reaches the end.

CodeStory's visualiser shows every comparison and swap, 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 the early exit. Then try a reversed list to see the worst case.

How Bubble Sort Moves the Largest Number

Steps below follow a pass on your screen.

  1. 1Compare two neighbours.
  2. 2Swap them when the left one is bigger.
  3. 3Finish the pass, and the largest remaining number sits at the end.
  4. 4Repeat on the shorter list and stop after a pass with no swaps.

Bubble Sort Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Without the early exitAlways makes n − 1 passes, even after a pass with no swaps. Results stay the same, but on a nearly sorted list the comparisons pile up.

Bubble Sort Cost in Time and Space

Nested passes compare neighbours many times, which makes the cost quadratic.

Time
O(n²)counted from the comparisons and swaps you step through
Space
O(1)swaps happen 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

Bubble Sort Visualiser FAQs

Open the visualiser and follow the passes swap by swap.

Open the visualiser
What does the bubble sort visualiser show?

CodeStory's visualiser shows every comparison and swap, pass by pass, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of bubble sort?

O(n²) time and O(1) space. Live counts show the comparisons piling up as the list grows.

How does bubble sort know when to stop?

No swaps in a pass means the list is sorted, so the sort stops there. Switch off the early exit in the visualiser to see the extra passes.

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.