- Visualize how sorting algorithms work with colored vertical bars
- When a bar is blue, it is inactive. This means that bar is unsorted and is currently not being compared.
- When a bar is red, that means that it is being compared with the other red bar.
- When a bar is purple, that means that it is in its final sorted position.
- All of the code related to the calculation and display is in ./src/SortingVisualizer
- All of the front end is contained in the React component
SortingVisualizer - All of the sorting algorithms are implemented and member functions of
SortingVisualizer - The animations are implemented as a member function of
SortingVisualizerthat gets called once a sorting algorithm has completed
- Each sorting algorithm is its own member function of
SortingVisualizer - A copy is made of the underlying state array
arraycalledtempArray - During the sorting algorithm, all computation and editing is done on
tempArray, not onarray - This is because
setState, which would be needed to editarray, is asyncronous, and does not guarantee completion before the next line of code. - You can read more about this here
- On every comparison, swap, or final sorted position, an object for animation gets pushed to the
animationarray - For more information about how the animations work, look here
- Animations are stored in an array in the state of
SortingVisualizercalledanimations - This array gets populated during a given sort with a sequence of objects
- These objects are in the form
{type: <type of animation>, indices: <indices of arrayBars that the animation applies to>} - The types are as follows:
- "compare", with the indices of bars being compared
- "swap", with the indices of bars to be swapped
- "sorted", with the indices of bars that are in their final sorted position
- The animations get displayed after the sorting terminated, by using
SetTimeout()to show the animations synchronously - The state variable
array, which contains the underlying array that gets sorted, is edited during the swap animation - This is because the bars get their height directly from
array - This means that the timeout has to be long enough for
setState()to be guaranteed to be completed before the next call. Otherwise, the state may not be changed before the next swap