-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (114 loc) · 3.34 KB
/
script.js
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
133
134
135
136
137
138
139
140
141
142
/*
Sorting algoritms visualizer by Håvard Hjelmeseth
*/
var svg = d3.select("svg"); // Select svg
// Dimensions of svg element
var margin = 30;
var width = document.getElementById('svg').clientWidth - 2 * margin;
var height = svg.attr("height") - 2 * margin;
// Create scale on the y axis
var yScale = d3.scaleLinear()
.domain([0, 100])
.range([height, 0]);
var yAxis = svg.append("g")
.attr("transform", "translate(" + margin + ", " + margin + ")")
.call(d3.axisLeft(yScale));
// Create a group that contains the rectangles
var rectangleGroup = svg.append("g")
.attr("id", "rectangleGroup")
.attr("transform", "translate(" + margin + ", " + margin + ")");
data = []; // Array containing heights of rectangles
var sorting = false; // Boolean to hinder sorting while already sorting
/*
UI FUNCTIONS
*/
// Draws rectangles from an array containing heights, calculates width automatically based on size of svg and length of array
function drawRectangles(array) {
svg.select("#rectangleGroup").selectAll("rect").remove()
for (i = 0; i < array.length; i++) {
rectangleGroup.append("rect")
.attr("class", "box")
.attr("width", (width - 20) / array.length)
.attr("height", array[i] * (height / 100))
.attr("x", 10 + i * (width / array.length))
.attr("y", height - (array[i] * (height / 100)))
.attr("fill", "#6c757e")
.attr("style", "stroke: black; stroke-width: 1;")
}
}
// Disables buttons while sorting
function disableButtons() {
var buttons = document.getElementsByTagName('button');
for (var i = 0; i < 2; i++) { //Replace 2 with buttons.length eventually
buttons[i].classList.add("disabled");
buttons[i].disabled = true;
}
}
// Enables buttons after sorting
function enableButtons() {
var buttons = document.getElementsByTagName('button');
for (var i = 0; i < 2; i++) { //Replace 2 with buttons.length eventually
buttons[i].classList.remove("disabled");
buttons[i].disabled = false;
}
}
/*
GENERATOR FUNCTIONS
*/
//Generates a random array of size "size"
function generateArray(size) {
arr = []
for (i = 0; i < size; i++) {
arr.push(Math.random() * 100)
}
return arr
}
// Generates new data and draws it
function drawData() {
data = generateArray(80)
drawRectangles(data)
}
/*
SORTING FUNCTIONS
*/
// Bubblesort
function* bubbleSort(array) { // Generator function
var did_swap = false;
var step = 0;
do {
did_swap = false;
for (var i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
did_swap = true;
step++;
var temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
drawRectangles(array);
yield did_swap;
}
}
} while (did_swap);
// Runs after sorting is finished
sorting = false
enableButtons();
}
function start_bubbleSort(option) {
var sort = bubbleSort(data);
function animate() {
requestAnimationFrame(animate);
sort.next();
}
animate(data);
}
// Mergesort
/*
HANDLE BUTTON PRESSES
*/
function handleBubblesort() {
if (sorting === false) {
sorting = true
disableButtons();
start_bubbleSort();
}
}