Algorithms Visualised · Computer science

Bubble sort

Bubble sort is an interactive experiment in Algorithms Visualised, a free simulation from StudyFi Lab that runs right in your browser.

Bubble sort compares neighbouring elements and swaps them when they are in the wrong order. After each pass the largest element has “bubbled up” to the end, so the unsorted part gets shorter. If a pass makes no swap, the array is sorted. In the worst case it needs n(n−1)/2 comparisons, which is O(n²).

Try this: Choose reversed input and compare the comparisons with n(n−1)/2. Then try a nearly sorted array — it stops after a few passes.

Frequently asked questions

What does the “Bubble sort” experiment show?

Bubble sort compares neighbouring elements and swaps them when they are in the wrong order. After each pass the largest element has “bubbled up” to the end, so the unsorted part gets shorter. If a pass makes no swap, the array is sorted. In the worst case it needs n(n−1)/2 comparisons, which is O(n²).

How do I try “Bubble sort”?

Choose reversed input and compare the comparisons with n(n−1)/2. Then try a nearly sorted array — it stops after a few passes.

Studying for a test?

Upload your notes and StudyFi turns them into flashcards, tests and podcasts.

Try StudyFi free