Algorithms Visualised
Algorithms Visualised is a free interactive simulation from StudyFi Lab. It runs right in your browser, with no sign-up and nothing to install.
Sorting and searching, step by step
Experiments
Bubble sort
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.
Insertion sort
Insertion sort works like sorting cards in your hand: the left part is already sorted, and each next element (the key) moves left until it meets a smaller one. Larger elements shift one place to the right on the way. On random data it is O(n²), but on nearly sorted data almost O(n) — a sorted array needs only n−1 comparisons.
Try this: Run it on a nearly sorted array, then on a reversed one. How many times more shifts does the reversed array need?
Merge sort
Merge sort splits the array in half, sorts each half the same way (recursion) and then merges the two sorted halves: it always takes the smaller of the two front elements. Halving gives log₂ n levels and every level handles all n elements, so the running time is O(n log n) in every case. On a tie it takes the left element, so it is stable.
Try this: Try a reversed and then a nearly sorted array of the same size. The comparisons barely change — the input order hardly matters.
Quicksort
Quicksort picks a pivot — here the last element of the range (Lomuto partition) — and rearranges the range so that smaller elements go left and the rest go right. The pivot then sits in its final place, and both parts are sorted the same way. On average it is O(n log n), but when the pivot splits badly (for example on an already sorted or reversed array) it drops to O(n²).
Try this: Compare random input with reversed input. On the reversed array the pivot is always the largest or smallest — watch the comparisons jump.
Linear vs binary search
Linear search checks the elements one by one, so in the worst case it needs n steps — O(n). Binary search only works on a sorted array: it looks at the middle and, depending on the result, throws away a whole half. After each step at most half is left, so it needs at most ⌈log₂(n+1)⌉ steps — O(log n). For 40 elements that is 6 steps instead of 40.
Try this: Set 40 elements and search for the last value. Then pick a number that is not in the array — how many steps does each search need to find out?
Sorting race
All four algorithms sort the same input, and in every lane one step is one comparison or one move. The O(n²) algorithms (bubble and insertion) grow much faster with the array size than the O(n log n) ones (merge and quick). The order also depends on the input: on a nearly sorted array insertion sort often wins.
Try this: Run the race on a random array of 40 elements, then switch to nearly sorted. Does the winner change?
Frequently asked questions
What is Algorithms Visualised?
Algorithms Visualised is a free interactive simulation from StudyFi Lab. It runs right in your browser, with no sign-up and nothing to install. Sorting and searching, step by step
Is Algorithms Visualised free?
Yes. Algorithms Visualised is completely free. It works in the browser on computers, tablets and phones, and you do not need an account.
What can I explore in Algorithms Visualised?
Bubble sort, Insertion sort, Merge sort, Quicksort, Linear vs binary search, Sorting race.
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²).
What does the “Insertion sort” experiment show?
Insertion sort works like sorting cards in your hand: the left part is already sorted, and each next element (the key) moves left until it meets a smaller one. Larger elements shift one place to the right on the way. On random data it is O(n²), but on nearly sorted data almost O(n) — a sorted array needs only n−1 comparisons.
Studying for a test?
Upload your notes and StudyFi turns them into flashcards, tests and podcasts.