Skip to content

Commit 27d6aa8

Browse files
authored
Add files via upload
1 parent 1e3dba9 commit 27d6aa8

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

041-sorting-algorithm-visualizer/index.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ <h1 class="title">
3636

3737
<div class="control-group">
3838
<label for="speed-control">Speed: <span id="speed-value">50</span>ms</label>
39-
<input type="range" id="speed-control" min="1" max="200" value="50" class="slider">
39+
<input type="range" id="speed-control" min="1" max="500" value="50" class="slider">
4040
</div>
4141

4242
<div class="button-group">
@@ -72,10 +72,6 @@ <h1 class="title">
7272
<div class="stat-label">Swaps</div>
7373
<div class="stat-value" id="swaps">0</div>
7474
</div>
75-
<div class="stat-card">
76-
<div class="stat-label">Time</div>
77-
<div class="stat-value" id="time">0.0s</div>
78-
</div>
7975
<div class="stat-card">
8076
<div class="stat-label">Complexity</div>
8177
<div class="stat-value" id="complexity">O(n²)</div>

041-sorting-algorithm-visualizer/script.js

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class SortingVisualizer {
1717
this.isRunning = false;
1818
this.isPaused = false;
1919
this.isSorted = false;
20+
this.shouldStop = false;
2021

2122
// Statistics
2223
this.comparisons = 0;
2324
this.swaps = 0;
24-
this.startTime = 0;
2525

2626
// Algorithm definitions with metadata
2727
this.algorithms = {
@@ -66,10 +66,24 @@ class SortingVisualizer {
6666
init() {
6767
this.cacheDOMElements();
6868
this.attachEventListeners();
69+
this.syncSliderValues();
6970
this.generateRandomArray();
7071
this.updateAlgorithmInfo();
7172
}
7273

74+
/**
75+
* Sync slider values with their display
76+
*/
77+
syncSliderValues() {
78+
// Sync array size
79+
this.arraySize = parseInt(this.arraySizeSlider.value);
80+
this.sizeValue.textContent = this.arraySize;
81+
82+
// Sync animation speed
83+
this.animationSpeed = parseInt(this.speedSlider.value);
84+
this.speedValue.textContent = this.animationSpeed;
85+
}
86+
7387
/**
7488
* Cache DOM elements for better performance
7589
*/
@@ -85,7 +99,6 @@ class SortingVisualizer {
8599
this.speedValue = document.getElementById('speed-value');
86100
this.comparisonsElement = document.getElementById('comparisons');
87101
this.swapsElement = document.getElementById('swaps');
88-
this.timeElement = document.getElementById('time');
89102
this.complexityElement = document.getElementById('complexity');
90103
this.algorithmName = document.getElementById('algorithm-name');
91104
this.algorithmDescription = document.getElementById('algorithm-description');
@@ -117,7 +130,22 @@ class SortingVisualizer {
117130
* Generate a new random array
118131
*/
119132
generateRandomArray() {
120-
if (this.isRunning) return;
133+
if (this.isRunning && !this.isPaused) return;
134+
135+
// If generating during pause, stop the current sorting
136+
if (this.isRunning && this.isPaused) {
137+
this.shouldStop = true;
138+
this.isRunning = false;
139+
this.isPaused = false;
140+
this.pauseBtn.disabled = true;
141+
this.pauseBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg> Pause';
142+
}
143+
144+
// Reset all visual states before generating new array
145+
const bars = this.barsContainer.children;
146+
for (let i = 0; i < bars.length; i++) {
147+
bars[i].classList.remove('comparing', 'sorted', 'pivot', 'selected');
148+
}
121149

122150
this.array = [];
123151
for (let i = 0; i < this.arraySize; i++) {
@@ -167,9 +195,8 @@ class SortingVisualizer {
167195

168196
this.isRunning = true;
169197
this.isPaused = false;
198+
this.shouldStop = false;
170199
this.resetStats();
171-
this.startTime = Date.now();
172-
this.updateTimer();
173200

174201
// Update UI state
175202
this.sortBtn.disabled = true;
@@ -201,12 +228,12 @@ class SortingVisualizer {
201228
break;
202229
}
203230

204-
// Mark all bars as sorted with animation
205-
await this.markAllSorted();
206-
207-
// Array is now sorted
208-
this.isSorted = true;
209-
this.sortBtn.disabled = true;
231+
// Only mark as sorted if still running (not interrupted)
232+
if (this.isRunning && !this.shouldStop) {
233+
await this.markAllSorted();
234+
this.isSorted = true;
235+
this.sortBtn.disabled = true;
236+
}
210237

211238
} catch (error) {
212239
console.error('Sorting interrupted:', error);
@@ -223,6 +250,10 @@ class SortingVisualizer {
223250
*/
224251
togglePause() {
225252
this.isPaused = !this.isPaused;
253+
254+
// Enable generate button during pause
255+
this.generateBtn.disabled = !this.isPaused;
256+
226257
this.pauseBtn.innerHTML = this.isPaused ?
227258
'<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"></polygon></svg> Resume' :
228259
'<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect></svg> Pause';
@@ -232,9 +263,15 @@ class SortingVisualizer {
232263
* Delay function that respects pause state
233264
*/
234265
async delay() {
235-
while (this.isPaused) {
266+
while (this.isPaused && !this.shouldStop) {
236267
await new Promise(resolve => setTimeout(resolve, 100));
237268
}
269+
270+
// Check if should stop
271+
if (this.shouldStop) {
272+
throw new Error('Sorting stopped');
273+
}
274+
238275
return new Promise(resolve => setTimeout(resolve, this.animationSpeed));
239276
}
240277

@@ -264,6 +301,11 @@ class SortingVisualizer {
264301
* Compare two elements with animation
265302
*/
266303
async compare(i, j) {
304+
// Check if should stop
305+
if (this.shouldStop) {
306+
throw new Error('Sorting stopped');
307+
}
308+
267309
const bars = this.barsContainer.children;
268310

269311
// Highlight bars being compared
@@ -601,17 +643,6 @@ class SortingVisualizer {
601643
this.swapsElement.textContent = this.swaps;
602644
}
603645

604-
/**
605-
* Update timer display
606-
*/
607-
updateTimer() {
608-
if (this.isRunning) {
609-
const elapsed = (Date.now() - this.startTime) / 1000;
610-
this.timeElement.textContent = elapsed.toFixed(1) + 's';
611-
requestAnimationFrame(() => this.updateTimer());
612-
}
613-
}
614-
615646
/**
616647
* Update algorithm information panel
617648
*/

0 commit comments

Comments
 (0)