33 text ,
44 maxElement = 15 ,
55 dataRange = maxElement * 2 ,
6- areaHeight = 150 ,
6+ areaHeight = 250 ,
77 areaWidth = 800 ,
88 time = 300 ,
99 traverseColor = "#ffcaa1" ,
@@ -13,6 +13,11 @@ var svg,
1313 isSorting = false ,
1414 isSorted = false ;
1515
16+ var swooshAudio = new Audio ( "./sound-effects/swoosh.mp3" ) ;
17+ var completeAudio = new Audio ( "./sound-effects/complete.mp3" ) ;
18+ swooshAudio . volume = 0.3 ;
19+ completeAudio . volume = 0.3 ;
20+
1621// generating random data
1722var data = randomData ( maxElement , dataRange ) ;
1823function setSpeed ( ) {
@@ -27,139 +32,141 @@ var heightScale = d3
2732// initialized a chart with random value
2833createChart ( data ) ;
2934
30- const selectionS = {
31- selectionSort ( ) {
35+ // javascript objects for performing different sorting algorithm
36+ const SortAlgo = {
37+ // bubble sort methods to perform bubble sort algorithm
38+ bubbleSort ( ) {
39+ // promise for async bubble sort with delay
40+
3241 const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
42+ // async function for bubble sort
3343
3444 async function sort ( self ) {
35- for ( let i = 0 ; i < data . length ; i ++ ) {
45+ var temp ;
46+ for ( let i = 0 ; i < data . length - 1 ; i ++ ) {
47+ // If user click on stop button then this function will stop performing here.
3648 if ( self . abort ) {
3749 self . abort = false ;
3850 return ;
3951 }
40- smallest = data [ i ] ;
41- pos = i ;
42- changeBarColor ( smallest , smallestColor ) ;
52+ // changing initial smallest bar color
53+ changeBarColor ( data [ 0 ] , smallestColor ) ;
4354 await timer ( time ) ;
44- for ( var j = i + 1 ; j < data . length ; j ++ ) {
55+ for ( j = 0 ; j < data . length - i - 1 ; j ++ ) {
56+ // If user click on stop button then this function will stop performing here.
4557 if ( self . abort ) {
4658 self . abort = false ;
59+ changeBarColor ( data [ j ] , unsortedColor ) ;
4760 return ;
4861 }
49- changeBarColor ( data [ j ] , traverseColor ) ;
50- if ( smallest > data [ j ] ) {
62+ await timer ( time ) ;
63+ changeBarColor ( data [ j + 1 ] , traverseColor ) ;
64+ await timer ( time ) ;
65+ if ( data [ j ] > data [ j + 1 ] ) {
66+ temp = data [ j ] ;
67+ data [ j ] = data [ j + 1 ] ;
68+ data [ j + 1 ] = temp ;
69+ changeBarColor ( data [ j + 1 ] , smallestColor ) ;
70+ swooshAudio . play ( ) ;
71+ swapBar ( data ) ;
5172 await timer ( time ) ;
52- changeBarColor ( smallest , unsortedColor ) ;
53- smallest = data [ j ] ;
54- pos = j ;
73+ } else {
74+ changeBarColor ( data [ j + 1 ] , smallestColor ) ;
5575 }
56-
57- changeBarColor ( smallest , smallestColor ) ;
58- await timer ( time ) ;
5976 changeBarColor ( data [ j ] , unsortedColor ) ;
6077 }
61- if ( data [ i ] != smallest ) {
62- temp = data [ i ] ;
63- data [ i ] = smallest ;
64- data [ pos ] = temp ;
65-
66- var swooshAudio = new Audio ( "./sound-effects/swoosh.mp3" ) ;
67- swooshAudio . play ( ) ;
68- }
69- changeBarColor ( smallest , sortedColor ) ;
70- swapBar ( data ) ;
71- await timer ( time ) ; // then the created Promise can be awaited
78+ changeBarColor ( data [ j ] , sortedColor ) ;
7279 }
80+
81+ // after complete sorting complete making all the bar green and playing complete sound effects
7382 svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
74- var completeAudio = new Audio ( "./sound-effects/complete.mp3" ) ;
83+
7584 completeAudio . play ( ) ;
7685 isSorting = false ;
7786 isSorted = true ;
7887 togglePlay ( ) ;
7988 }
8089
90+ // calling async function here
8191 sort ( this ) ;
8292 } ,
8393
84- selectionSortStop ( ) {
85- this . abort = true ;
86- isSorting = false ;
87- } ,
88- } ;
89-
90- const bubbleS = {
91- bubbleSort ( ) {
94+ // selection sort methods
95+ selectionSort ( ) {
96+ // promise for async selection sort with delay
9297 const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
9398
99+ // async function for selection sort algorithm
94100 async function sort ( self ) {
95- var temp ;
96- for ( let i = 0 ; i < data . length - 1 ; i ++ ) {
101+ for ( let i = 0 ; i < data . length ; i ++ ) {
102+ // Stoping execution here if users wants to stop.
97103 if ( self . abort ) {
98104 self . abort = false ;
99105 return ;
100106 }
101-
102- changeBarColor ( data [ 0 ] , smallestColor ) ;
107+ smallest = data [ i ] ;
108+ pos = i ;
109+ changeBarColor ( smallest , smallestColor ) ;
103110 await timer ( time ) ;
104- for ( j = 0 ; j < data . length - i - 1 ; j ++ ) {
111+ for ( var j = i + 1 ; j < data . length ; j ++ ) {
105112 if ( self . abort ) {
106113 self . abort = false ;
107- changeBarColor ( data [ j ] , unsortedColor ) ;
108114 return ;
109115 }
110- await timer ( time ) ;
111- changeBarColor ( data [ j + 1 ] , traverseColor ) ;
112- await timer ( time ) ;
113- if ( data [ j ] > data [ j + 1 ] ) {
114- temp = data [ j ] ;
115- data [ j ] = data [ j + 1 ] ;
116- data [ j + 1 ] = temp ;
117- changeBarColor ( data [ j + 1 ] , smallestColor ) ;
118- var swooshAudio = new Audio ( "./sound-effects/swoosh.mp3" ) ;
119- swooshAudio . play ( ) ;
120- swapBar ( data ) ;
116+ changeBarColor ( data [ j ] , traverseColor ) ;
117+ if ( smallest > data [ j ] ) {
121118 await timer ( time ) ;
122- } else {
123- changeBarColor ( data [ j + 1 ] , smallestColor ) ;
119+ changeBarColor ( smallest , unsortedColor ) ;
120+ smallest = data [ j ] ;
121+ pos = j ;
124122 }
123+
124+ changeBarColor ( smallest , smallestColor ) ;
125+ await timer ( time ) ;
125126 changeBarColor ( data [ j ] , unsortedColor ) ;
126127 }
127- changeBarColor ( data [ j ] , sortedColor ) ;
128+ if ( data [ i ] != smallest ) {
129+ temp = data [ i ] ;
130+ data [ i ] = smallest ;
131+ data [ pos ] = temp ;
132+ // playing swapping sound
133+ swooshAudio . play ( ) ;
134+ }
135+ // swapping bar and changing smallest color
136+ changeBarColor ( smallest , sortedColor ) ;
137+ swapBar ( data ) ;
138+ await timer ( time ) ; // then the created Promise can be awaited
128139 }
140+
141+ // After complete sorting algorithm making all the bar green.
129142 svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
130- var completeAudio = new Audio ( "./sound-effects/complete.mp3" ) ;
143+
131144 completeAudio . play ( ) ;
132145 isSorting = false ;
133146 isSorted = true ;
134147 togglePlay ( ) ;
135148 }
136-
149+ // calling sort function here
137150 sort ( this ) ;
138151 } ,
139152
140- bubbleSortStop ( ) {
153+ // If user wants to stop the sorting process then this function will be called and sorting algorithm will be stopped immediately.
154+ sortStop ( ) {
141155 this . abort = true ;
142156 isSorting = false ;
143157 } ,
144158} ;
145159
146160function stopSorting ( ) {
147- const stopBubbleSort = bubbleS . bubbleSortStop . bind ( bubbleS ) ;
148- const stopSelectionSort = selectionS . selectionSortStop . bind ( selectionS ) ;
149- if ( running == "bubble" ) {
150- stopBubbleSort ( ) ;
151- } else if ( running == "selection" ) {
152- stopSelectionSort ( ) ;
153- }
161+ const stopSorting = SortAlgo . sortStop . bind ( SortAlgo ) ;
162+ stopSorting ( ) ;
154163}
155164function startSorting ( ) {
156165 if ( getAlgo ( ) == "bubble-sort" ) {
157- const bubbleSortStarted = bubbleS . bubbleSort . bind ( bubbleS ) ;
158- running = "bubble" ;
166+ const bubbleSortStarted = SortAlgo . bubbleSort . bind ( SortAlgo ) ;
159167 bubbleSortStarted ( ) ;
160168 } else if ( getAlgo ( ) == "selection-sort" ) {
161- const selectionSortStarted = selectionS . selectionSort . bind ( selectionS ) ;
162- running = "selection" ;
169+ const selectionSortStarted = SortAlgo . selectionSort . bind ( SortAlgo ) ;
163170 selectionSortStarted ( ) ;
164171 }
165172}
@@ -190,3 +197,14 @@ document.getElementById("random-data").addEventListener("click", function () {
190197 var data = randomData ( maxElement , dataRange ) ;
191198 createChart ( data ) ;
192199} ) ;
200+
201+ document . getElementById ( "sound" ) . addEventListener ( "click" , function ( ) {
202+ if ( this . classList . contains ( "line-through" ) ) {
203+ swooshAudio . volume = 0.3 ;
204+ completeAudio . volume = 0.3 ;
205+ } else {
206+ swooshAudio . volume = 0 ;
207+ completeAudio . volume = 0 ;
208+ }
209+ this . classList . toggle ( "line-through" ) ;
210+ } ) ;
0 commit comments