|
20 | 20 | path { |
21 | 21 | transition: transform 500ms; |
22 | 22 | } |
23 | | - #controls, #controls>*{ |
24 | | - position: relative; |
| 23 | + #controls { |
25 | 24 | min-width: 200px; |
26 | 25 | width: 100%; |
27 | 26 | } |
| 27 | + #controls, #controls>*{ |
| 28 | + position: relative; |
| 29 | + } |
| 30 | + #progress-bar{ |
| 31 | + position: relative; |
| 32 | + } |
28 | 33 | </style> |
29 | 34 |
|
30 | 35 | <div id="controls"> |
31 | 36 | <button id="shuffleBtn" type="button">Shuffle</button> |
32 | 37 | <button id="quickSortBtn" type="button">Quick Sort</button> |
33 | 38 | <button id="heapSortBtn" type="button">Heap Sort</button> |
| 39 | + <button id="combSortBtn" type="button">Comb Sort</button> |
34 | 40 | <button id="insertionSortBtn" type="button">Insertion Sort (slow)</button> |
35 | | - <br><hr><br> |
| 41 | + <br> |
| 42 | + <label>Swap delay (ms): <input type="number" id="swapDelayMs" value="1"></label> |
| 43 | + <br> |
| 44 | + <label>Transform delay (ms): <input type="number" id="transformDelayMs" value="500"></label> |
| 45 | + <hr> |
36 | 46 | </div> |
| 47 | + <!-- <div id="progress-bar"></div> --> |
37 | 48 | <script type="module" defer> |
38 | 49 | import { |
39 | 50 | quickSort, |
40 | 51 | insertionSort, |
41 | 52 | selectionSort, |
42 | 53 | heapSort, |
| 54 | + combSort, |
43 | 55 | } from './sorting-algorithms-ui.js' |
44 | 56 |
|
45 | 57 | document.addEventListener('DOMContentLoaded', async function() { |
46 | | - const delayMs = 10; |
47 | | - document.getElementById('insertionSortBtn').addEventListener('click', async function(e) { |
48 | | - elems = await insertionSort(elems, {cmp, swap}); |
49 | | - console.log('done sorting'); |
| 58 | + let delayMs = 1; |
| 59 | + document.getElementById('swapDelayMs').addEventListener('change', function(e) { |
| 60 | + delayMs = e.target.value; |
| 61 | + }); |
| 62 | + document.getElementById('transformDelayMs').addEventListener('change', function(e) { |
| 63 | + document.querySelectorAll('path').forEach(el=>el.style.transition = `transform ${e.target.value}ms`); |
50 | 64 | }); |
51 | 65 | document.getElementById('quickSortBtn').addEventListener('click', async function(e) { |
52 | 66 | elems = await quickSort(elems, {cmp, swap}); |
|
56 | 70 | elems = await heapSort(elems, {cmp, swap}); |
57 | 71 | console.log('done sorting'); |
58 | 72 | }); |
| 73 | + document.getElementById('combSortBtn').addEventListener('click', async function(e) { |
| 74 | + elems = await combSort(elems, {cmp, swap}); |
| 75 | + console.log('done sorting'); |
| 76 | + }); |
| 77 | + document.getElementById('insertionSortBtn').addEventListener('click', async function(e) { |
| 78 | + elems = await insertionSort(elems, {cmp, swap}); |
| 79 | + console.log('done sorting'); |
| 80 | + }); |
59 | 81 | document.getElementById('shuffleBtn').addEventListener('click', async function(e) { |
60 | 82 | elems = await shuffle(elems); |
61 | 83 | console.log('done shuffling'); |
|
69 | 91 | elem.setAttribute('data-originalPos', getPos(elem).join(" ")); |
70 | 92 | }); |
71 | 93 |
|
| 94 | + // function updateProgress(op, perc) { |
| 95 | + // document.getElementById('progress-bar').innerHTML = `${op}: ${(perc*100).toFixed(1)}% [${Array(Math.floor(perc*100/4)).fill('█').join('')}${Array(Math.ceil((100-(100*perc))/4)).fill('░').join('')}]`; |
| 96 | + // } |
| 97 | + |
72 | 98 | function cmp(e1, e2) { |
73 | 99 | let leftVal = parseInt(e1.id.replace('path', '')) |
74 | 100 | let rightVal = parseInt(e2.id.replace('path', '')) |
|
87 | 113 | // Pick a remaining element. |
88 | 114 | randomIndex = randInt(currentIndex); |
89 | 115 | currentIndex--; |
| 116 | + // updateProgress('Shuffling', (arr.length-currentIndex) / arr.length); |
90 | 117 | await swap(arr, currentIndex, randomIndex, 1); |
91 | 118 | } |
92 | 119 | return arr; |
|
0 commit comments