-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.htm
132 lines (115 loc) · 4.32 KB
/
index.htm
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Quick sort alogrithm</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<p>Hello World</p>
<form>
<p>Number of elements:</p>
<input type="range" name="amount" min="3" max="200" value="3" step="1" class="range_1">
<p class="range_1_val">1</p>
<p>Scale:</p>
<input type="range" name="amount" min="1" max="100" value="1" step="5" class="range_2">
<input type="number" name="Bar Weight" , value="5" class="weight">
<p class="range_2_val">1</p>
<input type="button" id="submit_btn" value="Submit and randomize">
<input type="button" id="start_btn_bubble" value="Start Bubble Sort">
</form>
<br>
<canvas id="can" width="1000" height="500" style="border: 1px solid black">Canvas not supported</canvas>
<script>
var canvas = document.getElementById('can');
var ctx = canvas.getContext('2d');
var scale = 10;
var weight = 5;
var elementsArray = [];
var i = 0;
var j = 0;
function wait(ms) {
var d = new Date();
var d2 = null;
do { d2 = new Date(); }
while (d2 - d < ms);
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function arrangeElementsVisual(array) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < elementsArray.length; i++) {
ctx.fillRect(0, i * (weight), array[i] * scale, weight);
}
}
function gameLoopBubble() {
//Bubble sort
if (i < elementsArray.length - 1) {
if (j < elementsArray.length - i - 1) {
if (elementsArray[j] > elementsArray[j + 1]) {
const temp = elementsArray[j];
elementsArray[j] = elementsArray[j + 1]
elementsArray[j + 1] = temp;
}
j++;
} else {
j = 0;
i++;
}
arrangeElementsVisual(elementsArray)
} else {
clearInterval(window.inter)
console.log('hello')
}
}
function gameLoopInsert() {
//Insertion Sort
if (i < elementsArray.length) {
if (j >= 0 && elementsArray[j] > key) {
elementsArray[j + 1] = elementsArray[j];
j = j - 1;
console.log(elementsArray)
} else {
elementsArray[j + 1] = key;
i++;
key = elementsArray[i];
j = i - 1;
}
} else {
clearInterval(window.inter)
console.log('hello')
}
}
document.getElementById('submit_btn').addEventListener('click', function () {
elementsArray = []
for (let i = 1; i <= document.getElementsByClassName('range_1')[0].value; i++) {
elementsArray.push(i)
}
scale = document.getElementsByClassName('range_2')[0].value
weight = parseInt(document.getElementsByClassName('weight')[0].value)
shuffleArray(elementsArray)
console.log(elementsArray)
arrangeElementsVisual(elementsArray)
})
document.getElementById('start_btn_bubble').addEventListener('click', function () {
i = 0;
j = 0;
window.inter = setInterval(gameLoopBubble, 1)
})
document.getElementById('start_btn_insert').addEventListener('click', function () {
i = 1;
key = elementsArray[i]
j = i - 1;
window.inter = setInterval(gameLoopBubble, 100)
})
</script>
</body>
</html>