@@ -9,12 +9,12 @@ var svg,
9
9
traverseColor = "#ffcaa1" ,
10
10
smallestColor = "#ab87ff" ,
11
11
unsortedColor = "#add8e6" ,
12
- sortedColor = "#56b4d3 " ,
12
+ sortedColor = "green " ,
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" ) ;
16
+ var swooshAudio = new Audio ( "./../ sound-effects/swoosh.mp3" ) ;
17
+ var completeAudio = new Audio ( "./../ sound-effects/complete.mp3" ) ;
18
18
swooshAudio . volume = 0.3 ;
19
19
completeAudio . volume = 0.3 ;
20
20
@@ -32,249 +32,111 @@ var heightScale = d3
32
32
// initialized a chart with random value
33
33
createChart ( data ) ;
34
34
35
- // javascript objects for performing different sorting algorithm
36
- const SortAlgo = {
37
- // bubble sort methods to perform bubble sort algorithm
38
- bubbleSort ( ) {
35
+ const SearchAlgo = {
36
+ liearSearch ( ) {
39
37
// promise for async bubble sort with delay
40
38
41
39
const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
42
40
// async function for bubble sort
43
41
44
- async function sort ( self ) {
42
+ async function search ( self ) {
45
43
var temp ;
46
44
for ( let i = 0 ; i < data . length - 1 ; i ++ ) {
47
45
// If user click on stop button then this function will stop performing here.
48
- if ( self . abort ) {
49
- self . abort = false ;
50
- return ;
51
- }
46
+
52
47
// changing initial smallest bar color
53
- changeBarColor ( data [ 0 ] , smallestColor ) ;
54
48
await timer ( time ) ;
55
- for ( j = 0 ; j < data . length - i - 1 ; j ++ ) {
56
- // If user click on stop button then this function will stop performing here.
57
- if ( self . abort ) {
58
- self . abort = false ;
59
- changeBarColor ( data [ j ] , unsortedColor ) ;
60
- return ;
61
- }
62
- await timer ( time ) ;
63
- changeBarColor ( data [ j + 1 ] , traverseColor ) ;
49
+ changeBarColor ( data [ i ] , traverseColor ) ;
50
+ console . log ( data [ i ] ) ;
51
+ await timer ( time ) ;
52
+ console . log ( "Searching" ) ;
53
+
54
+ if ( data [ i ] == target ) {
55
+ changeBarColor ( data [ i ] , sortedColor ) ;
56
+ console . log ( "found" ) ;
64
57
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 ) ;
72
- await timer ( time ) ;
73
- } else {
74
- changeBarColor ( data [ j + 1 ] , smallestColor ) ;
75
- }
76
- changeBarColor ( data [ j ] , unsortedColor ) ;
58
+ break ;
77
59
}
78
- changeBarColor ( data [ j ] , sortedColor ) ;
79
60
}
80
61
81
62
// after complete sorting complete making all the bar green and playing complete sound effects
82
- svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
83
63
84
64
completeAudio . play ( ) ;
85
65
isSorting = false ;
86
66
isSorted = true ;
87
- togglePlay ( ) ;
88
67
}
89
68
90
69
// calling async function here
91
- sort ( this ) ;
70
+ search ( this ) ;
92
71
} ,
93
72
94
- // selection sort methods
95
- selectionSort ( ) {
96
- // promise for async selection sort with delay
73
+ binarySearch ( ) {
74
+ // promise for async bubble sort with delay
75
+
97
76
const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
77
+ // async function for bubble sort
98
78
99
- // async function for selection sort algorithm
100
- async function sort ( self ) {
101
- for ( let i = 0 ; i < data . length ; i ++ ) {
102
- // Stoping execution here if users wants to stop.
103
- if ( self . abort ) {
104
- self . abort = false ;
105
- return ;
106
- }
107
- smallest = data [ i ] ;
108
- pos = i ;
109
- changeBarColor ( smallest , smallestColor ) ;
79
+ async function search ( self ) {
80
+ console . log ( target ) ;
81
+ let l = 0 ,
82
+ r = data . length - 1 ,
83
+ mid ;
84
+ while ( l <= r ) {
85
+ // If user click on stop button then this function will stop performing here.
86
+ mid = ( l + r ) / 2 ;
110
87
await timer ( time ) ;
111
- for ( var j = i + 1 ; j < data . length ; j ++ ) {
112
- if ( self . abort ) {
113
- self . abort = false ;
114
- return ;
115
- }
116
- changeBarColor ( data [ j ] , traverseColor ) ;
117
- if ( smallest > data [ j ] ) {
118
- await timer ( time ) ;
119
- changeBarColor ( smallest , unsortedColor ) ;
120
- smallest = data [ j ] ;
121
- pos = j ;
122
- }
123
-
124
- changeBarColor ( smallest , smallestColor ) ;
88
+ changeBarColor ( data [ mid ] , traverseColor ) ;
89
+ if ( data [ mid ] == target ) {
90
+ changeBarColor ( data [ mid ] , sortedColor ) ;
91
+ console . log ( "found" ) ;
125
92
await timer ( time ) ;
126
- changeBarColor ( data [ j ] , unsortedColor ) ;
127
- }
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
139
- }
140
-
141
- // After complete sorting algorithm making all the bar green.
142
- svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
143
-
144
- completeAudio . play ( ) ;
145
- isSorting = false ;
146
- isSorted = true ;
147
- togglePlay ( ) ;
148
- }
149
- // calling sort function here
150
- sort ( this ) ;
151
- } ,
152
-
153
- //Merge Sort methods to perform merge sort algorithm
154
- mergeSort ( ) {
155
- const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
156
-
157
- // async function for selection sort algorithm
158
- async function sort ( self , arr , l , r ) {
159
- // l is for left index and r is
160
- // right index of the sub-array
161
- // of arr to be sorted */
162
- if ( r > l ) {
163
- var m = l + parseInt ( ( r - l ) / 2 ) ;
164
-
165
- sort ( this , arr , l , m ) ;
166
-
167
- sort ( this , arr , m + 1 , r ) ;
168
-
169
- var n1 = m - l + 1 ;
170
- var n2 = r - m ;
171
-
172
- // Create temp arrays
173
- var L = new Array ( n1 ) ;
174
- var R = new Array ( n2 ) ;
175
-
176
- // Copy data to temp arrays L[] and R[]
177
- for ( var i = 0 ; i < n1 ; i ++ ) L [ i ] = arr [ l + i ] ;
178
- for ( var j = 0 ; j < n2 ; j ++ ) R [ j ] = arr [ m + 1 + j ] ;
179
-
180
- // Merge the temp arrays back into arr[l..r]
181
-
182
- // Initial index of first subarray
183
- var i = 0 ;
184
-
185
- // Initial index of second subarray
186
- var j = 0 ;
187
-
188
- // Initial index of merged subarray
189
- var k = l ;
190
-
191
- while ( i < n1 && j < n2 ) {
192
- if ( L [ i ] <= R [ j ] ) {
193
- arr [ k ] = L [ i ] ;
194
- i ++ ;
195
- } else {
196
- arr [ k ] = R [ j ] ;
197
- j ++ ;
198
- }
199
- k ++ ;
200
- }
201
-
202
- // Copy the remaining elements of
203
- // L[], if there are any
204
- while ( i < n1 ) {
205
- arr [ k ] = L [ i ] ;
206
- i ++ ;
207
- k ++ ;
93
+ break ;
94
+ } else if ( data [ mid ] < target ) {
95
+ l = mid + 1 ;
96
+ } else {
97
+ r = mid - 1 ;
208
98
}
99
+ // changing initial smallest bar color
209
100
210
- // Copy the remaining elements of
211
- // R[], if there are any
212
- while ( j < n2 ) {
213
- arr [ k ] = R [ j ] ;
214
- j ++ ;
215
- k ++ ;
216
- }
217
- swapBar ( data ) ;
101
+ await timer ( time ) ;
218
102
}
219
103
220
- console . log ( data ) ;
221
- svg . selectAll ( "rect" ) . style ( "fill" , "#56b4d3" ) ;
104
+ // after complete sorting complete making all the bar green and playing complete sound effects
105
+
222
106
completeAudio . play ( ) ;
223
107
isSorting = false ;
224
108
isSorted = true ;
225
- togglePlay ( ) ;
226
109
}
227
110
228
- // calling sort function here
229
- sort ( this , data , 0 , data . length - 1 ) ;
230
- } ,
231
-
232
- // If user wants to stop the sorting process then this function will be called and sorting algorithm will be stopped immediately.
233
- sortStop ( ) {
234
- this . abort = true ;
235
- isSorting = false ;
111
+ // calling async function here
112
+ search ( this ) ;
236
113
} ,
237
114
} ;
238
115
239
- function stopSorting ( ) {
240
- const stopSorting = SortAlgo . sortStop . bind ( SortAlgo ) ;
241
- stopSorting ( ) ;
242
- }
243
- function startSorting ( ) {
244
- if ( getAlgo ( ) == "bubble-sort" ) {
245
- const bubbleSortStarted = SortAlgo . bubbleSort . bind ( SortAlgo ) ;
246
- bubbleSortStarted ( ) ;
247
- } else if ( getAlgo ( ) == "selection-sort" ) {
248
- const selectionSortStarted = SortAlgo . selectionSort . bind ( SortAlgo ) ;
249
- selectionSortStarted ( ) ;
116
+ function startSearching ( ) {
117
+ if ( getAlgo ( ) == "linear-search" ) {
118
+ const linearSearchStarted = SearchAlgo . liearSearch . bind ( SearchAlgo ) ;
119
+ linearSearchStarted ( ) ;
120
+ } else if ( getAlgo ( ) == "binary-search" ) {
121
+ const binarySearchStarted = SearchAlgo . binarySearch . bind ( SearchAlgo ) ;
122
+ binarySearchStarted ( ) ;
250
123
} else if ( getAlgo ( ) == "merge-sort" ) {
251
124
const mergeSortStarted = SortAlgo . mergeSort . bind ( SortAlgo ) ;
252
125
mergeSortStarted ( ) ;
253
126
}
254
127
}
255
128
256
- document . getElementById ( "sort" ) . addEventListener ( "click" , function ( ) {
257
- isSorting = true ;
258
- startSorting ( ) ;
259
- togglePlay ( ) ;
260
- } ) ;
129
+ document . getElementById ( "search" ) . addEventListener ( "click" , function ( ) {
130
+ target = parseInt ( document . getElementById ( "targetValue" ) . value ) ;
261
131
262
- document . getElementById ( "stop" ) . addEventListener ( "click" , function ( ) {
263
- if ( isSorting ) {
264
- stopSorting ( ) ;
265
- togglePlay ( ) ;
132
+ if ( isNaN ( target ) ) {
133
+ alert ( "Please enter a valid number" ) ;
134
+ } else {
135
+ startSearching ( ) ;
266
136
}
267
137
} ) ;
268
138
269
139
document . getElementById ( "random-data" ) . addEventListener ( "click" , function ( ) {
270
- if ( isSorting ) {
271
- stopSorting ( ) ;
272
- togglePlay ( ) ;
273
- }
274
- if ( isSorted ) {
275
- isSorted = false ;
276
- document . getElementById ( "sort" ) . classList . remove ( "none" ) ;
277
- }
278
140
svg . remove ( ) ;
279
141
var data = randomData ( maxElement , dataRange ) ;
280
142
createChart ( data ) ;
0 commit comments