# How can I animate the sorting algorithm used by Sort? [duplicate]

I try to make an animation of the sorting steps taken by Sort like this but something goes wrong:

list = RandomInteger[100, 20]
step = 1;
Animate[BarChart[Sort[list, (step++ < limit && #1 < #2) &]], {limit,
1, 100}, AnimationRunning -> False]

• Output all steps of Sort is perhaps the closest to your question, in that it implements the visualization as an animated bar chart. Also relevant: How can I collect data for visualization of quick sort?. – MarcoB Mar 27 '19 at 21:38
• I disagree about this being marked a duplicate. None of the linked answers actually show how to visualise the sorting algorithm employed by Sort. The linked answers all implement their own sorting algorithms, which is not what is being asked here. – Sjoerd Smit Mar 28 '19 at 9:26
• Animate[BarChart[ Block[{step = 1}, Sort[list, (#1 < #2 \[Implies] step++ > limit) &]]], {limit, 1, 100}, AnimationRunning -> False] – Michael E2 Jul 21 '20 at 17:50

You were almost there:

list = RandomInteger[100, 10];
Animate[
Block[{step = 1},
BarChart[Sort[list, (step++ >= limit || OrderedQ[{#1, #2}]) &]]
],
{limit, 1, 100, 1},
AnimationRunning -> False
]


Very nice idea, I must say. I don't know if I would've come up with that on my own.