Algorithm Lab: Sorting & Searching Visualizers and Games
Free sorting and searching visualizers for students: bubble, selection, insertion, merge and quick sort, linear vs binary search, and algorithm efficiency.
Common questions
What is Algorithm Lab?
Algorithm Lab is a free collection of 3 interactive visualizers and 2 games from CS++ for learning sorting, searching and algorithm efficiency. You can step through five sorting algorithms forward and backward, compare linear search with binary search on the same list, see how the number of steps grows for different kinds of algorithms, and practice with a number-guessing game and a name-that-sort game. Everything runs in the browser.
How does each sorting algorithm work?
Bubble sort compares neighbors and swaps any pair that is out of order, pass after pass, until a pass makes no swaps. Selection sort finds the smallest value in the unsorted part and swaps it to the front of that part. Insertion sort takes the next value and slides it left until it sits in order among the values before it. Merge sort splits the list in half, sorts each half the same way, and merges the two sorted halves. Quicksort picks a pivot, moves smaller values to its left and larger values to its right, and then sorts each side the same way.
Which sorting algorithm is the fastest?
It depends on the list. For a long list in random order, merge sort and quicksort are much faster than bubble, selection and insertion sort, because their steps grow about as n log n instead of n squared. Merge sort takes about n log n steps in every case. Quicksort usually does too, but in its worst case it takes about n squared. On a list that is already nearly sorted, insertion sort does the least work of the five, needing close to n comparisons.
Should I use linear search or binary search?
Use binary search when the list is sorted: it checks the middle item, throws away the half that cannot hold the target, and repeats, so even a million items need at most 20 checks. Use linear search when the list is not sorted or is short: it checks every item in order and works on any list. Binary search on an unsorted list can give a wrong answer.
Why does binary search need sorted data?
Binary search compares the target with the middle item and then discards one whole half. That is only safe if every item on one side is smaller than the middle and every item on the other side is larger, which is what sorted means. In an unsorted list the target can be sitting in the half that gets thrown away, so binary search can report that a value is missing when it is there.
How many steps does binary search take?
In the worst case binary search checks floor(log2 n) + 1 items, because every check leaves at most half of the items. That is 4 checks for 15 items, 7 for 100, 10 for 1,000 and 20 for 1,000,000. Each time the list doubles, the worst case grows by just one check.
What does "reasonable time" mean in AP CSP?
In AP Computer Science Principles, an algorithm runs in a reasonable amount of time when the number of steps it takes grows as a polynomial of the input size or slower: constant, linear, or the size squared, for example. An algorithm whose steps grow exponentially or factorially runs in an unreasonable amount of time, because the work becomes impossible even for modest inputs.
What is a heuristic?
A heuristic is an approach that finds a solution that is good enough but is not guaranteed to be the best one. It is used when finding the best solution would take an unreasonable amount of time. For example, a delivery route can be built by always driving to the nearest stop not yet visited: that is fast and usually close to the shortest route, but not always the shortest.