DSAVisualiseData Structures & Algorithms Studio
Sorting & Selection Hard

Constrained In-Place Sort

CardSorter bubble-sorts a linked list that exposes only two moves: exchange the first two cards, or rotate the first card to the tail.

Trace input

Bounded JSON

Edit the lesson input, then build a deterministic source-linked trace.

This lesson validates its own input and operation limits.

Source scenarios

Complexity

BestO(n²)
AverageO(n²)
WorstO(n²)
SpaceO(1) extra, plus the linked nodes

Array state

Follow the active comparisons and writes.

Live snapshot

Current operation

Trace state
Ready to explore
Source behavior & common mistakes

Follow each adjacent comparison, optional swap, and mandatory rotation. The source repeats a pass and rotates the prefix back so the next pass starts at the list head.

  • Every inner comparison is followed by pop(), even when the pair is already ordered.
  • swap() exchanges only the first two nodes; it is not an arbitrary-position swap.
  • The cleanup loop rotates the remaining prefix and does not compare or swap cards.
  • The source swaps only when first > second, so equal-valued card identities remain stable.
  • The pinned implementation throws on a two-card list during the final rotation; the trace preserves this source edge case.

Algorithm code

Java
CardSorter.java (pinned repository source)Step linked
The highlighted line follows the current step.

Ready to begin

Press Play to see what happens.

Detailed execution
Snapshot 1 / 1
Writes 0