3
3
text ,
4
4
maxElement = 15 ,
5
5
dataRange = maxElement * 2 ,
6
- areaHeight = 150 ,
6
+ areaHeight = 250 ,
7
7
areaWidth = 800 ,
8
8
time = 300 ,
9
9
traverseColor = "#ffcaa1" ,
@@ -13,6 +13,11 @@ var svg,
13
13
isSorting = false ,
14
14
isSorted = false ;
15
15
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
+
16
21
// generating random data
17
22
var data = randomData ( maxElement , dataRange ) ;
18
23
function setSpeed ( ) {
@@ -27,139 +32,141 @@ var heightScale = d3
27
32
// initialized a chart with random value
28
33
createChart ( data ) ;
29
34
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
+
32
41
const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
42
+ // async function for bubble sort
33
43
34
44
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.
36
48
if ( self . abort ) {
37
49
self . abort = false ;
38
50
return ;
39
51
}
40
- smallest = data [ i ] ;
41
- pos = i ;
42
- changeBarColor ( smallest , smallestColor ) ;
52
+ // changing initial smallest bar color
53
+ changeBarColor ( data [ 0 ] , smallestColor ) ;
43
54
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.
45
57
if ( self . abort ) {
46
58
self . abort = false ;
59
+ changeBarColor ( data [ j ] , unsortedColor ) ;
47
60
return ;
48
61
}
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 ) ;
51
72
await timer ( time ) ;
52
- changeBarColor ( smallest , unsortedColor ) ;
53
- smallest = data [ j ] ;
54
- pos = j ;
73
+ } else {
74
+ changeBarColor ( data [ j + 1 ] , smallestColor ) ;
55
75
}
56
-
57
- changeBarColor ( smallest , smallestColor ) ;
58
- await timer ( time ) ;
59
76
changeBarColor ( data [ j ] , unsortedColor ) ;
60
77
}
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 ) ;
72
79
}
80
+
81
+ // after complete sorting complete making all the bar green and playing complete sound effects
73
82
svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
74
- var completeAudio = new Audio ( "./sound-effects/complete.mp3" ) ;
83
+
75
84
completeAudio . play ( ) ;
76
85
isSorting = false ;
77
86
isSorted = true ;
78
87
togglePlay ( ) ;
79
88
}
80
89
90
+ // calling async function here
81
91
sort ( this ) ;
82
92
} ,
83
93
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
92
97
const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
93
98
99
+ // async function for selection sort algorithm
94
100
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.
97
103
if ( self . abort ) {
98
104
self . abort = false ;
99
105
return ;
100
106
}
101
-
102
- changeBarColor ( data [ 0 ] , smallestColor ) ;
107
+ smallest = data [ i ] ;
108
+ pos = i ;
109
+ changeBarColor ( smallest , smallestColor ) ;
103
110
await timer ( time ) ;
104
- for ( j = 0 ; j < data . length - i - 1 ; j ++ ) {
111
+ for ( var j = i + 1 ; j < data . length ; j ++ ) {
105
112
if ( self . abort ) {
106
113
self . abort = false ;
107
- changeBarColor ( data [ j ] , unsortedColor ) ;
108
114
return ;
109
115
}
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 ] ) {
121
118
await timer ( time ) ;
122
- } else {
123
- changeBarColor ( data [ j + 1 ] , smallestColor ) ;
119
+ changeBarColor ( smallest , unsortedColor ) ;
120
+ smallest = data [ j ] ;
121
+ pos = j ;
124
122
}
123
+
124
+ changeBarColor ( smallest , smallestColor ) ;
125
+ await timer ( time ) ;
125
126
changeBarColor ( data [ j ] , unsortedColor ) ;
126
127
}
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
128
139
}
140
+
141
+ // After complete sorting algorithm making all the bar green.
129
142
svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
130
- var completeAudio = new Audio ( "./sound-effects/complete.mp3" ) ;
143
+
131
144
completeAudio . play ( ) ;
132
145
isSorting = false ;
133
146
isSorted = true ;
134
147
togglePlay ( ) ;
135
148
}
136
-
149
+ // calling sort function here
137
150
sort ( this ) ;
138
151
} ,
139
152
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 ( ) {
141
155
this . abort = true ;
142
156
isSorting = false ;
143
157
} ,
144
158
} ;
145
159
146
160
function 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 ( ) ;
154
163
}
155
164
function startSorting ( ) {
156
165
if ( getAlgo ( ) == "bubble-sort" ) {
157
- const bubbleSortStarted = bubbleS . bubbleSort . bind ( bubbleS ) ;
158
- running = "bubble" ;
166
+ const bubbleSortStarted = SortAlgo . bubbleSort . bind ( SortAlgo ) ;
159
167
bubbleSortStarted ( ) ;
160
168
} else if ( getAlgo ( ) == "selection-sort" ) {
161
- const selectionSortStarted = selectionS . selectionSort . bind ( selectionS ) ;
162
- running = "selection" ;
169
+ const selectionSortStarted = SortAlgo . selectionSort . bind ( SortAlgo ) ;
163
170
selectionSortStarted ( ) ;
164
171
}
165
172
}
@@ -190,3 +197,14 @@ document.getElementById("random-data").addEventListener("click", function () {
190
197
var data = randomData ( maxElement , dataRange ) ;
191
198
createChart ( data ) ;
192
199
} ) ;
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