|
| 1 | +function CreateRects() { |
| 2 | + elem = `<svg width="10" id="rectup"> |
| 3 | + <rect width="10" id="rectdown" style="fill:rgb(255,255,255);stroke-width:1;stroke:rgb(0,0,0)"/> |
| 4 | + </svg>` |
| 5 | + for (let i = 0; i < 100; i++) { |
| 6 | + document.getElementById('rects').insertAdjacentHTML('afterbegin', elem) |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +CreateRects() |
| 11 | + |
| 12 | +let x = document.querySelectorAll("#rectup"); |
| 13 | +let y = document.querySelectorAll("#rectdown"); |
| 14 | + |
| 15 | +function sleep(ms) { |
| 16 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 17 | +} |
| 18 | + |
| 19 | +function GetRandomHeights() { |
| 20 | + random_heights = [] |
| 21 | + |
| 22 | + for (let i = 10; i < 110; i++) { |
| 23 | + random_heights.push(i); |
| 24 | + } |
| 25 | + |
| 26 | + return random_heights |
| 27 | +} |
| 28 | + |
| 29 | +function Randomize() { |
| 30 | + random_heights = GetRandomHeights() |
| 31 | + |
| 32 | + for (const rctx of x) { |
| 33 | + let random_nr = random_heights[Math.floor(Math.random() * random_heights.length)]; |
| 34 | + rctx.style.height = random_nr |
| 35 | + random_heights.splice(random_heights.indexOf(random_nr), 1); |
| 36 | + } |
| 37 | + |
| 38 | + document.getElementById('status').innerHTML = 'Randomized' |
| 39 | +} |
| 40 | + |
| 41 | +async function Sort() { |
| 42 | + Analyzing() |
| 43 | + |
| 44 | + await sleep(1000) |
| 45 | + |
| 46 | + number = 10 |
| 47 | + |
| 48 | + document.getElementById('status').innerHTML = 'Sorting'; |
| 49 | + |
| 50 | + for (let z = 0; z < 100; z++) { |
| 51 | + for (rctx of x) { |
| 52 | + if (rctx.style.height == number + 'px') { |
| 53 | + ordered_rect = x[number-10] |
| 54 | + replacement_rect = rctx.style.height |
| 55 | + ordered_replacement = ordered_rect.style.height |
| 56 | + |
| 57 | + // console.log(rctx.style.height, ordered_rect.style.height) |
| 58 | + |
| 59 | + ordered_rect.style.height = replacement_rect |
| 60 | + rctx.style.height = ordered_replacement |
| 61 | + |
| 62 | + number += 1 |
| 63 | + await sleep(50) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + Sorted() |
| 68 | +} |
| 69 | + |
| 70 | +async function Analyzing() { |
| 71 | + document.getElementById('status').innerHTML = 'Analyzing'; |
| 72 | + |
| 73 | + for (const rcty of y) { |
| 74 | + rcty.style = 'fill:rgb(255,0,0);stroke-width:1;stroke:rgb(0,0,0)'; |
| 75 | + await sleep(10); |
| 76 | + } |
| 77 | + |
| 78 | + for (const rcty of y) { |
| 79 | + rcty.style = 'fill:rgb(255,255,255);stroke-width:1;stroke:rgb(0,0,0)'; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +async function Sorted() { |
| 84 | + for (const rcty of y) { |
| 85 | + rcty.style = 'fill:rgb(0,255,0);stroke-width:1;stroke:rgb(0,0,0)'; |
| 86 | + await sleep(10); |
| 87 | + } |
| 88 | + |
| 89 | + for (const rcty of y) { |
| 90 | + rcty.style = 'fill:rgb(255,255,255);stroke-width:1;stroke:rgb(0,0,0)'; |
| 91 | + } |
| 92 | + |
| 93 | + document.getElementById('status').innerHTML = 'Sorted'; |
| 94 | + } |
0 commit comments