-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (67 loc) · 2.09 KB
/
index.js
File metadata and controls
86 lines (67 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { sleep } from "./helpers/util.js";
import { SortingAlgorithms } from "./helpers/sortingAlgorithms.js";
let nBars = 10
let numbersBars = document.getElementById('numbersBars')
const stage = document.getElementById('stage')
stage.style.width = `${nBars * 30}px`
const selectAlgorithm = document.getElementById('selectAlgorithm')
const generateBtn = document.getElementById('generateBtn')
const solveBtn = document.getElementById('solveBtn')
let bars = []
let barsDivs = []
const sortingAlgorithms = new SortingAlgorithms({})
const start = () => {
stage.innerHTML = ''
bars = Array(nBars).fill(0).map(_ => {
return {
width: 20,
height: Math.floor(Math.random() * 200) + 1
}
})
barsDivs = []
for (let i = 0; i < bars.length; i++) {
const bar = document.createElement('div')
bar.style.width = `${bars[i].width}px`
bar.style.height = `${bars[i].height}px`
bar.style.left = `${5 + i * 30}px`
bars[i] = { ...bars[i], position: i }
bar.classList.add('bar')
barsDivs.push(bar)
stage.appendChild(bar)
}
}
start()
async function swapBars(barsDivs, i, j) {
barsDivs[i].style.left = `${5 + j * 30}px`
barsDivs[i].classList.add('activate')
barsDivs[j].style.left = `${5 + i * 30}px`
barsDivs[j].classList.add('activate')
await sleep(300)
barsDivs[i].classList.remove('activate')
barsDivs[j].classList.remove('activate')
let temp = barsDivs[i]
barsDivs[i] = barsDivs[j]
barsDivs[j] = temp
}
const algorithms = [
sortingAlgorithms.bubbleSort,
sortingAlgorithms.selectionSort,
sortingAlgorithms.quickSort
]
const solve = async () => {
const array = structuredClone(bars.map(el => el.height))
const swaps = algorithms[selectAlgorithm.selectedIndex](array)
for (let i = 0; i < swaps.length; i++) {
if (swaps[i].firstPostion !== swaps[i].lastPosition) {
await swapBars(barsDivs, swaps[i].firstPostion, swaps[i].lastPosition)
}
}
}
generateBtn.addEventListener('click', () => {
nBars = parseInt(numbersBars.value, 10)
stage.style.width = `${nBars * 30}px`
start()
})
solveBtn.addEventListener('click', () => {
solve()
})