|
| 1 | + |
| 2 | +function clearCanvas() { |
| 3 | + ctx.beginPath(); |
| 4 | + ctx.fillStyle = BACKGROUND_COLOR; |
| 5 | + ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 6 | + ctx.closePath(); |
| 7 | +} |
| 8 | + |
| 9 | +function resetArray() { |
| 10 | + array = []; |
| 11 | +} |
| 12 | + |
| 13 | +async function sleep() { |
| 14 | + return new Promise((temp) => { |
| 15 | + setTimeout(() => temp(2), SPEED); // timeout this function for duration SPEED |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +function swap(i, j) { |
| 20 | + let temp = array[i]; |
| 21 | + array[i] = array[j]; |
| 22 | + array[j] = temp; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * |
| 27 | + * @param {index in array to draw} index |
| 28 | + * @param {The value of the index in array} value |
| 29 | + * @param {The new color to draw the index} color |
| 30 | + */ |
| 31 | +function drawBar(index, value, color = DEFAULT_COLOR) { |
| 32 | + ctx.beginPath(); |
| 33 | + // clear the previous bar |
| 34 | + ctx.fillStyle = BACKGROUND_COLOR; |
| 35 | + ctx.fillRect( |
| 36 | + Math.ceil((WIDTH / ARR_SIZE) * index), |
| 37 | + 0, |
| 38 | + Math.floor(WIDTH / ARR_SIZE), |
| 39 | + HEIGHT |
| 40 | + ); |
| 41 | + |
| 42 | + ctx.fillStyle = color; |
| 43 | + ctx.fillRect( |
| 44 | + Math.ceil((WIDTH / ARR_SIZE) * index), |
| 45 | + HEIGHT, |
| 46 | + Math.floor(WIDTH / ARR_SIZE), |
| 47 | + Math.floor(-HEIGHT * (value / MAX_ARR_VAL)) |
| 48 | + ); |
| 49 | + |
| 50 | + ctx.closePath(); |
| 51 | +} |
| 52 | + |
| 53 | +function drawArray() { |
| 54 | + clearCanvas(); |
| 55 | + |
| 56 | + for (let i = 0; i < array.length; i++) { |
| 57 | + let val = array[i]; |
| 58 | + drawBar(i, val, DEFAULT_COLOR); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function randomizeArray() { |
| 63 | + if (running) { |
| 64 | + running = false; |
| 65 | + } |
| 66 | + clearCanvas(); |
| 67 | + resetArray(); |
| 68 | + |
| 69 | + for (let i = 0; i < ARR_SIZE; i++) { |
| 70 | + array.push(Math.floor(Math.random() * MAX_ARR_VAL)); |
| 71 | + } |
| 72 | + |
| 73 | + drawArray(); |
| 74 | +} |
| 75 | + |
| 76 | +function runSort() { |
| 77 | + |
| 78 | + if (!running) { |
| 79 | + running = true; |
| 80 | + |
| 81 | + let sortingAlgorithm; |
| 82 | + const algoName = sortingAlgo.value; |
| 83 | + switch (algoName) { |
| 84 | + case "bubble": |
| 85 | + sortingAlgorithm = bubbleSort; |
| 86 | + break; |
| 87 | + case "selection": |
| 88 | + sortingAlgorithm = selectionSort; |
| 89 | + break; |
| 90 | + case "insertion": |
| 91 | + sortingAlgorithm = insertionSort; |
| 92 | + break; |
| 93 | + case "merge": |
| 94 | + sortingAlgorithm = mergeSort; |
| 95 | + break; |
| 96 | + case "quick": // TODO |
| 97 | + sortingAlgorithm = quickSort; |
| 98 | + break; |
| 99 | + case "heap": |
| 100 | + sortingAlgorithm = heapSort; |
| 101 | + break; |
| 102 | + default: |
| 103 | + console.log("Invalid input"); |
| 104 | + } |
| 105 | + |
| 106 | + sortingAlgorithm(array).then((sorted) => { |
| 107 | + if (running) { |
| 108 | + array = sorted; |
| 109 | + drawArray(); |
| 110 | + running = false; |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + } else { |
| 115 | + console.log("Cannot restart: sorting is actively running!"); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function speedInput() { |
| 120 | + SPEED = speedSlider.value; |
| 121 | + speedOutput.innerHTML = SPEED; |
| 122 | +} |
| 123 | +function sizeInput() { |
| 124 | + ARR_SIZE = sizeSlider.value; |
| 125 | + sizeOutput.innerHTML = ARR_SIZE; |
| 126 | +} |
| 127 | + |
| 128 | +var canvas = document.getElementById("myCanvas"); |
| 129 | +var ctx = canvas.getContext("2d"); |
| 130 | +const WIDTH = canvas.width, |
| 131 | + HEIGHT = canvas.height; |
| 132 | + |
| 133 | +// button, slider setup |
| 134 | +randomizeBtn = document.getElementById("randomizeBtn"); |
| 135 | +sortingBtn = document.getElementById("sortButton"); |
| 136 | + |
| 137 | +// array size |
| 138 | +var sizeSlider = document.getElementById("sizeSlider"); |
| 139 | +var sizeOutput = document.getElementById("sizeValue"); |
| 140 | +var ARR_SIZE; |
| 141 | +// getting latency |
| 142 | +var speedSlider = document.getElementById("speedSlider"); |
| 143 | +var speedOutput = document.getElementById("speedValue"); // get the output from the slider span |
| 144 | +var SPEED; |
| 145 | +// getting sorting algorithm |
| 146 | +var sortingAlgo = document.getElementById("sortingFunction"); |
| 147 | + |
| 148 | +// everytime we mess with slider, it updates values inside |
| 149 | +speedSlider.oninput = speedInput; |
| 150 | +sizeSlider.oninput = () => { |
| 151 | + sizeInput(); |
| 152 | + |
| 153 | + randomizeArray(); |
| 154 | +}; |
| 155 | + |
| 156 | + |
| 157 | +// initializing stuff I use everywhere |
| 158 | +let array = []; |
| 159 | +const MAX_ARR_VAL = 100; |
| 160 | +let running = false; |
| 161 | + |
| 162 | +// COLORS |
| 163 | +const DEFAULT_COLOR = "black"; |
| 164 | +const BACKGROUND_COLOR = "white"; |
| 165 | + |
| 166 | +// first call |
| 167 | +speedInput(); |
| 168 | +sizeInput(); |
| 169 | +randomizeArray(); // randomize and draw the array |
0 commit comments