@@ -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