|
| 1 | +import React from 'react'; |
| 2 | +import {getMergeSortAnimations} from '../sortingAlgorithms/sortingAlgorithms.js'; |
| 3 | +import './SortingVisualizer.css'; |
| 4 | + |
| 5 | +// Change this value for the speed of the animations. |
| 6 | +const ANIMATION_SPEED_MS = 1; |
| 7 | + |
| 8 | +// Change this value for the number of bars (value) in the array. |
| 9 | +const NUMBER_OF_ARRAY_BARS = 310; |
| 10 | + |
| 11 | +// This is the main color of the array bars. |
| 12 | +const PRIMARY_COLOR = 'turquoise'; |
| 13 | + |
| 14 | +// This is the color of array bars that are being compared throughout the animations. |
| 15 | +const SECONDARY_COLOR = 'red'; |
| 16 | + |
| 17 | +export default class SortingVisualizer extends React.Component { |
| 18 | + constructor(props) { |
| 19 | + super(props); |
| 20 | + |
| 21 | + this.state = { |
| 22 | + array: [], |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + componentDidMount() { |
| 27 | + this.resetArray(); |
| 28 | + } |
| 29 | + |
| 30 | + resetArray() { |
| 31 | + const array = []; |
| 32 | + for (let i = 0; i < NUMBER_OF_ARRAY_BARS; i++) { |
| 33 | + array.push(randomIntFromInterval(5, 730)); |
| 34 | + } |
| 35 | + this.setState({array}); |
| 36 | + } |
| 37 | + |
| 38 | + mergeSort() { |
| 39 | + const animations = getMergeSortAnimations(this.state.array); |
| 40 | + for (let i = 0; i < animations.length; i++) { |
| 41 | + const arrayBars = document.getElementsByClassName('array-bar'); |
| 42 | + const isColorChange = i % 3 !== 2; |
| 43 | + if (isColorChange) { |
| 44 | + const [barOneIdx, barTwoIdx] = animations[i]; |
| 45 | + const barOneStyle = arrayBars[barOneIdx].style; |
| 46 | + const barTwoStyle = arrayBars[barTwoIdx].style; |
| 47 | + const color = i % 3 === 0 ? SECONDARY_COLOR : PRIMARY_COLOR; |
| 48 | + setTimeout(() => { |
| 49 | + barOneStyle.backgroundColor = color; |
| 50 | + barTwoStyle.backgroundColor = color; |
| 51 | + }, i * ANIMATION_SPEED_MS); |
| 52 | + } else { |
| 53 | + setTimeout(() => { |
| 54 | + const [barOneIdx, newHeight] = animations[i]; |
| 55 | + const barOneStyle = arrayBars[barOneIdx].style; |
| 56 | + barOneStyle.height = `${newHeight}px`; |
| 57 | + }, i * ANIMATION_SPEED_MS); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + quickSort() { |
| 63 | + // We leave it as an exercise to the viewer of this code to implement this method. |
| 64 | + } |
| 65 | + |
| 66 | + heapSort() { |
| 67 | + // We leave it as an exercise to the viewer of this code to implement this method. |
| 68 | + } |
| 69 | + |
| 70 | + bubbleSort() { |
| 71 | + // We leave it as an exercise to the viewer of this code to implement this method. |
| 72 | + } |
| 73 | + |
| 74 | + // NOTE: This method will only work if your sorting algorithms actually return |
| 75 | + // the sorted arrays; if they return the animations (as they currently do), then |
| 76 | + // this method will be broken. |
| 77 | + testSortingAlgorithms() { |
| 78 | + for (let i = 0; i < 100; i++) { |
| 79 | + const array = []; |
| 80 | + const length = randomIntFromInterval(1, 1000); |
| 81 | + for (let i = 0; i < length; i++) { |
| 82 | + array.push(randomIntFromInterval(-1000, 1000)); |
| 83 | + } |
| 84 | + const javaScriptSortedArray = array.slice().sort((a, b) => a - b); |
| 85 | + const mergeSortedArray = getMergeSortAnimations(array.slice()); |
| 86 | + console.log(arraysAreEqual(javaScriptSortedArray, mergeSortedArray)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + render() { |
| 91 | + const {array} = this.state; |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="array-container"> |
| 95 | + {array.map((value, idx) => ( |
| 96 | + <div |
| 97 | + className="array-bar" |
| 98 | + key={idx} |
| 99 | + style={{ |
| 100 | + backgroundColor: PRIMARY_COLOR, |
| 101 | + height: `${value}px`, |
| 102 | + }}></div> |
| 103 | + ))} |
| 104 | + <button onClick={() => this.resetArray()}>Generate New Array</button> |
| 105 | + <button onClick={() => this.mergeSort()}>Merge Sort</button> |
| 106 | + <button onClick={() => this.quickSort()}>Quick Sort</button> |
| 107 | + <button onClick={() => this.heapSort()}>Heap Sort</button> |
| 108 | + <button onClick={() => this.bubbleSort()}>Bubble Sort</button> |
| 109 | + <button onClick={() => this.testSortingAlgorithms()}> |
| 110 | + Test Sorting Algorithms (BROKEN) |
| 111 | + </button> |
| 112 | + </div> |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// From https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript |
| 118 | +function randomIntFromInterval(min, max) { |
| 119 | + // min and max included |
| 120 | + return Math.floor(Math.random() * (max - min + 1) + min); |
| 121 | +} |
| 122 | + |
| 123 | +function arraysAreEqual(arrayOne, arrayTwo) { |
| 124 | + if (arrayOne.length !== arrayTwo.length) return false; |
| 125 | + for (let i = 0; i < arrayOne.length; i++) { |
| 126 | + if (arrayOne[i] !== arrayTwo[i]) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + } |
| 130 | + return true; |
| 131 | +} |
0 commit comments