Skip to content

Commit 0b0c0c7

Browse files
committed
Added stop button and fixed some issue with generating new data while sorting
1 parent 1c5a637 commit 0b0c0c7

File tree

4 files changed

+63
-99
lines changed

4 files changed

+63
-99
lines changed

base.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,15 @@ function swapBar(data) {
111111
return bandScale(d) + 10;
112112
});
113113
}
114+
115+
function togglePlay() {
116+
var sortElement = document.getElementById("sort");
117+
var stopElement = document.getElementById("stop");
118+
if (isSorted) {
119+
sortElement.classList.add("none");
120+
stopElement.classList.add("none");
121+
} else {
122+
sortElement.classList.toggle("none");
123+
stopElement.classList.toggle("none");
124+
}
125+
}

function.js

Lines changed: 39 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ var svg,
99
traverseColor = "#ffcaa1",
1010
smallestColor = "#ab87ff",
1111
unsortedColor = "#add8e6",
12-
sortedColor = "#56b4d3";
12+
sortedColor = "#56b4d3",
13+
isSorting = false,
14+
isSorted = false;
1315

1416
// generating random data
1517
var data = randomData(maxElement, dataRange);
@@ -24,84 +26,7 @@ var heightScale = d3
2426

2527
// initialized a chart with random value
2628
createChart(data);
27-
/*
28-
let Sort = new sortData(data);
29-
Sort.selectionSort = function () {
30-
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
31-
async function sort() {
32-
// We need to wrap the loop into an async function for this to work
33-
for (var i = 0; i < data.length; i++) {
34-
smallest = data[i];
35-
pos = i;
36-
changeBarColor(smallest, smallestColor);
37-
await timer(time);
38-
for (var j = i + 1; j < data.length; j++) {
39-
changeBarColor(data[j], traverseColor);
40-
if (smallest > data[j]) {
41-
await timer(time);
42-
changeBarColor(smallest, unsortedColor);
43-
smallest = data[j];
44-
pos = j;
45-
}
46-
47-
changeBarColor(smallest, smallestColor);
48-
await timer(time);
49-
changeBarColor(data[j], unsortedColor);
50-
}
51-
if (data[i] != smallest) {
52-
temp = data[i];
53-
data[i] = smallest;
54-
data[pos] = temp;
55-
56-
var swooshAudio = new Audio("./sound-effects/swoosh.mp3");
57-
swooshAudio.play();
58-
}
59-
changeBarColor(smallest, sortedColor);
60-
swapBar(data);
61-
await timer(time); // then the created Promise can be awaited
62-
}
63-
svg.selectAll("rect").style("fill", sortedColor);
64-
var completeAudio = new Audio("./sound-effects/complete.mp3");
65-
completeAudio.play();
66-
}
67-
sort();
68-
};
69-
70-
Sort.bubbleSort = function () {
71-
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
72-
async function sort() {
73-
var temp;
74-
for (i = 0; i < data.length - 1; i++) {
75-
changeBarColor(data[0], smallestColor);
76-
await timer(time);
77-
for (j = 0; j < data.length - i - 1; j++) {
78-
await timer(time);
79-
changeBarColor(data[j + 1], traverseColor);
80-
await timer(time);
81-
if (data[j] > data[j + 1]) {
82-
temp = data[j];
83-
data[j] = data[j + 1];
84-
data[j + 1] = temp;
85-
changeBarColor(data[j + 1], smallestColor);
86-
var swooshAudio = new Audio("./sound-effects/swoosh.mp3");
87-
swooshAudio.play();
88-
swapBar(data);
89-
await timer(time);
90-
} else {
91-
changeBarColor(data[j + 1], smallestColor);
92-
}
93-
changeBarColor(data[j], unsortedColor);
94-
}
95-
changeBarColor(data[j], sortedColor);
96-
}
9729

98-
svg.selectAll("rect").style("fill", "#56b4d3");
99-
var completeAudio = new Audio("./sound-effects/complete.mp3");
100-
completeAudio.play();
101-
}
102-
sort();
103-
};
104-
*/
10530
const selectionS = {
10631
selectionSort() {
10732
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
@@ -145,13 +70,20 @@ const selectionS = {
14570
swapBar(data);
14671
await timer(time); // then the created Promise can be awaited
14772
}
73+
svg.selectAll("rect").style("fill", "#56b4d3");
74+
var completeAudio = new Audio("./sound-effects/complete.mp3");
75+
completeAudio.play();
76+
isSorting = false;
77+
isSorted = true;
78+
togglePlay();
14879
}
14980

15081
sort(this);
15182
},
15283

15384
selectionSortStop() {
15485
this.abort = true;
86+
isSorting = false;
15587
},
15688
};
15789

@@ -162,7 +94,6 @@ const bubbleS = {
16294
async function sort(self) {
16395
var temp;
16496
for (let i = 0; i < data.length - 1; i++) {
165-
console.log(self.abort);
16697
if (self.abort) {
16798
self.abort = false;
16899
return;
@@ -195,58 +126,67 @@ const bubbleS = {
195126
}
196127
changeBarColor(data[j], sortedColor);
197128
}
129+
svg.selectAll("rect").style("fill", "#56b4d3");
130+
var completeAudio = new Audio("./sound-effects/complete.mp3");
131+
completeAudio.play();
132+
isSorting = false;
133+
isSorted = true;
134+
togglePlay();
198135
}
199136

200137
sort(this);
201138
},
202139

203140
bubbleSortStop() {
204141
this.abort = true;
142+
isSorting = false;
205143
},
206144
};
207145

208146
function stopSorting() {
209147
const stopBubbleSort = bubbleS.bubbleSortStop.bind(bubbleS);
210148
const stopSelectionSort = selectionS.selectionSortStop.bind(selectionS);
211-
212-
stopBubbleSort();
213-
stopSelectionSort();
149+
if (running == "bubble") {
150+
stopBubbleSort();
151+
} else if (running == "selection") {
152+
stopSelectionSort();
153+
}
214154
}
215155
function startSorting() {
216156
if (getAlgo() == "bubble-sort") {
217157
const bubbleSortStarted = bubbleS.bubbleSort.bind(bubbleS);
218-
console.log("clicked buble");
158+
running = "bubble";
219159
bubbleSortStarted();
220160
} else if (getAlgo() == "selection-sort") {
221161
const selectionSortStarted = selectionS.selectionSort.bind(selectionS);
222-
console.log("clicked Selection");
162+
running = "selection";
223163
selectionSortStarted();
224164
}
225165
}
226166

227167
document.getElementById("sort").addEventListener("click", function () {
168+
isSorting = true;
228169
startSorting();
170+
togglePlay();
229171
});
230172

231-
document.getElementById("random-data").addEventListener("click", function () {
232-
stopSorting();
233-
svg.remove();
234-
var data = randomData(maxElement, dataRange);
235-
createChart(data);
236-
});
237-
/*
238-
document.getElementById("sort").addEventListener("click", function () {
239-
if (getAlgo() == "selection-sort") {
240-
selectionS.selectionSort.bind(bubbleS);
241-
} else if (getAlgo() == "bubble-sort") {
242-
bubbleS.bubbleSort.bind(bubbleS);
173+
document.getElementById("stop").addEventListener("click", function () {
174+
if (isSorting) {
175+
stopSorting();
176+
togglePlay();
243177
}
244178
});
245179

246180
document.getElementById("random-data").addEventListener("click", function () {
247-
var data = randomData(maxElement, dataRange);
248-
Sort.stopSort();
181+
if (isSorting) {
182+
stopSorting();
183+
togglePlay();
184+
}
185+
if (isSorted) {
186+
isSorted = false;
187+
document.getElementById("sort").classList.remove("none");
188+
}
249189
svg.remove();
190+
var data = randomData(maxElement, dataRange);
250191
createChart(data);
251192
});
252-
*/

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ <h1>Sorting Visualizer</h1>
4747
<div id="chart"></div>
4848
<hr />
4949
<button id="sort">Sort</button>
50+
<button id="stop" class="none stop">Stop</button>
5051
</div>
5152
</div>
5253
<script src="https://d3js.org/d3.v4.min.js"></script>

style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,14 @@ input {
7676
rect:hover {
7777
cursor: pointer;
7878
}
79+
.none {
80+
display: none;
81+
}
82+
.stop {
83+
background-image: linear-gradient(
84+
to right,
85+
#ad4a04 0%,
86+
#d36756 51%,
87+
#8f5a34 100%
88+
);
89+
}

0 commit comments

Comments
 (0)