Prashnikaप्रश्निका
Code
About

Sorting visualiser

Type a short list and pick a sort. Press Play to watch the bars being compared, swapped and shifted, with the Java line that is running lit and a sentence saying what just happened. Use the arrow keys or the slider to go back and forth.

How to use it

  1. 1Pick bubble, selection or insertion sort and type 2 to 10 whole numbers, or tap an example.
  2. 2Press Show me, then Play. Each comparison, swap and shift is shown one at a time.
  3. 3Read the sentence under the bars and watch the Java line that is running. Use Back to see a step again.
Which sort

The animation needs JavaScript. Every step is written out below.

Result

Result
sorted: 10, 13, 14, 29, 37
Every step in words (25 steps)
  1. Start: the list is 29, 10, 14, 37, 13. Sort it into ascending order.
  2. Pass 1: assume the smallest value is at position 0 (a[0] = 29).
  3. Is a[1] < a[min]? 10 < 29 is true.
  4. True, so min = 1: the smallest so far is 10.
  5. Is a[2] < a[min]? 14 < 10 is false.
  6. Is a[3] < a[min]? 37 < 10 is false.
  7. Is a[4] < a[min]? 13 < 10 is false.
  8. Swap a[0] with a[min]: 29 and 10 change places.
  9. Pass 2: assume the smallest value is at position 1 (a[1] = 29).
  10. Is a[2] < a[min]? 14 < 29 is true.
  11. True, so min = 2: the smallest so far is 14.
  12. Is a[3] < a[min]? 37 < 14 is false.
  13. Is a[4] < a[min]? 13 < 14 is true.
  14. True, so min = 4: the smallest so far is 13.
  15. Swap a[1] with a[min]: 29 and 13 change places.
  16. Pass 3: assume the smallest value is at position 2 (a[2] = 14).
  17. Is a[3] < a[min]? 37 < 14 is false.
  18. Is a[4] < a[min]? 29 < 14 is false.
  19. The smallest value is already at position 2, so nothing moves.
  20. Pass 4: assume the smallest value is at position 3 (a[3] = 37).
  21. Is a[4] < a[min]? 29 < 37 is true.
  22. True, so min = 4: the smallest so far is 29.
  23. Swap a[3] with a[min]: 37 and 29 change places.
  24. Only one value is left, and it is already in place.
  25. Done. The sorted list is 10, 13, 14, 29, 37.
Start again

About the Sorting visualiser

What it does

Sorts your list in ascending order with the sort you pick and shows every step: which two values are being compared, which are swapped or shifted, and which are already in their final place. Each step has a sentence in words and the matching Java line marked in the code.

The three sorts

  • Bubble sort: compares neighbouring values and swaps them if they are in the wrong order; after each pass the largest remaining value has moved to the end. It stops early if a whole pass makes no swap.
  • Selection sort: finds the smallest value in the unsorted part and swaps it into the next place.
  • Insertion sort: takes the next value (the key) and shifts the larger values to its left one place right, then drops the key into the gap.

Common questions

What is the difference between bubble sort and selection sort?

Bubble sort swaps neighbouring values many times as the large values bubble to the end. Selection sort looks through the unsorted part to find the smallest value and makes just one swap per pass.

Why does my bubble sort stop early?

If a complete pass makes no swap, the list is already in order, so the sort can stop. Try the sorted-list example to see it.

Can I sort numbers that repeat?

Yes. Equal values are not swapped by bubble sort or insertion sort, so they keep their order.

Can I see the code in Python, JavaScript or C++?

Yes. Choose the language in the menu above the code. Only the listing changes: the steps are always worked out from the Java program that ISC uses, and the highlighted line moves to the matching line of the language you chose.

Practise on real ISC questions

More Computer Science tools

All study tools →