I want to make a bar visualisation of many sorting algorithms, I started will bubble sort for simplicity sake. I need to update the dom as it switches the items, but it only updates it after all the elements are sorted. I looked this up and everything said to use tick, but I still can't get it to work, here is my code:
<script>
import { tick } from 'svelte';
let arr = [];
async function bubbleSort () {
let len = arr.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
if (arr[j] > arr[j + 1]) {
let tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
await tick();
setTimeout(() => { console.log("World!"); }, 2000);
}
}
}
console.log(arr)
}
function shuffle () {
arr = [];
for (let i = 0; i < 100; i++) {
let num = Math.random()*10+1;
arr.push(Math.floor(num));
}
console.log(arr)
}
console.log(bubbleSort(shuffle()))
</script>
<main>
<button on:click={shuffle}>Shuffle</button>
<button on:click={bubbleSort}>Sort</button>
{#each arr as el, i}
<div id={i} style="position: relative;height: {el*100}px;"></div>
{/each}
</main>
<style>
div {
background: #000000;
width: 5px;
display: inline-block;
margin: 0 1px;
}
</style>
I am sorry if this is simple but I am very new to svelte, js and web dev as a whole. Thank you!