diff options
author | Anthony Wang | 2024-11-04 13:28:52 -0500 |
---|---|---|
committer | Anthony Wang | 2024-11-04 13:28:52 -0500 |
commit | 5e6592fc9644368234de5d7b56a0f42644c49c6c (patch) | |
tree | 4e1dc19bed7b6b892939a4226ac8a3b5e4b7e639 /layouts | |
parent | 4e9731c1b867f869734c2904735708d3d4a9ffec (diff) |
ICan'tBelieveItCanSort
Diffstat (limited to 'layouts')
-rw-r--r-- | layouts/shortcodes/sort.html | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/layouts/shortcodes/sort.html b/layouts/shortcodes/sort.html new file mode 100644 index 0000000..a748bd1 --- /dev/null +++ b/layouts/shortcodes/sort.html @@ -0,0 +1,36 @@ +<button onclick="run()">Run visualization</button> +<pre id="visualization"> +</pre> +<script> +let running = false + +function render(n, A, i, j) { + let line1 = Array(n).fill(" ") + line1[j] = "j" + line1[i] = "i" + document.getElementById("visualization").innerHTML = line1.join(" ") + "\n" + A.join(" ") +} + +async function run() { + if (running) return + running = true + + let n = 10 + let A = Array(n).fill().map(() => Math.floor(Math.random() * 10)) + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + render(n, A, i, j) + await new Promise(resolve => setTimeout(resolve, 500)) + if (A[i] < A[j]) { + let tmp = A[i] + A[i] = A[j] + A[j] = tmp + render(n, A, i, j) + await new Promise(resolve => setTimeout(resolve, 500)) + } + } + } + + running = false +} +</script> |