@@ -23,7 +23,7 @@ for (i = 0; i < 5; i++) {
23
23
slider . oninput = function ( ) {
24
24
slidevalue . innerHTML = this . value ;
25
25
numBoxes = this . value ;
26
- var obj = document . getElementsByClassName ( "box" ) ;
26
+ var obj = document . getElementsByClassName ( "box" ) ;
27
27
28
28
while ( obj [ 0 ] ) {
29
29
obj [ 0 ] . parentNode . removeChild ( obj [ 0 ] ) ;
@@ -69,9 +69,9 @@ function removePx (str) {
69
69
70
70
71
71
async function Swap ( x , y ) {
72
- if ( sortSelection = 0 ) {
72
+ if ( sortSelection == 0 ) {
73
73
await sleep ( ( - 22 ) * ( myCollection . length ) + 1120 ) ;
74
- } else if ( sortSelection = 1 ) {
74
+ } else if ( sortSelection == 1 ) {
75
75
await sleep ( 25 ) ;
76
76
}
77
77
var temp = myCollection [ x ] . style . height ;
@@ -159,8 +159,92 @@ function doQuickSort() {
159
159
QuickSort ( 0 , myCollection . length - 1 ) ;
160
160
}
161
161
162
+
162
163
// MergeSort
163
164
165
+ async function merge ( l , m , r ) {
166
+ await sleep ( 200 ) ;
167
+ // Find sizes of two subarrays to be merged
168
+ var n1 = m - l + 1 ;
169
+ var n2 = r - m ;
170
+
171
+
172
+
173
+ /* Create temp arrays */
174
+ let leftArray = new Array ( n1 ) ;
175
+ let rightArray = new Array ( n2 ) ;
176
+
177
+ /*Copy data to temp arrays*/
178
+ for ( i = 0 ; i < n1 ; ++ i ) {
179
+ leftArray [ i ] = removePx ( myCollection [ l + i ] . style . height ) ;
180
+ }
181
+
182
+ for ( j = 0 ; j < n2 ; ++ j ) {
183
+ rightArray [ j ] = removePx ( myCollection [ m + 1 + j ] . style . height ) ;
184
+
185
+ }
186
+
187
+ /* Merge the temp arrays */
188
+
189
+ // Initial indexes of first and second subarrays
190
+ var i = 0
191
+ var j = 0 ;
192
+
193
+ // Initial index of merged subarry array
194
+ var k = l ;
195
+
196
+ while ( ( i < n1 ) && ( j < n2 ) ) {
197
+ if ( leftArray [ i ] >= rightArray [ j ] ) {
198
+ myCollection [ k ] . style . height = addPx ( leftArray [ i ] ) ;
199
+ i ++ ;
200
+
201
+ } else {
202
+
203
+ myCollection [ k ] . style . height = addPx ( rightArray [ j ] ) ;
204
+
205
+ j ++ ;
206
+ }
207
+
208
+ k ++ ;
209
+ }
210
+
211
+ /* Copy remaining elements of L[] if any */
212
+ while ( i < n1 ) {
213
+ myCollection [ k ] . style . height = addPx ( leftArray [ i ] ) ;
214
+ i ++ ;
215
+ k ++ ;
216
+ }
217
+
218
+ /* Copy remaining elements of R[] if any */
219
+ while ( j < n2 ) {
220
+ myCollection [ k ] . style . height = addPx ( rightArray [ j ] ) ;
221
+ j ++ ;
222
+ k ++ ;
223
+
224
+ }
225
+
226
+
227
+ }
228
+
229
+ async function MergeSort ( l , r ) {
230
+ await sleep ( 50 ) ;
231
+ if ( l < r ) {
232
+ myCollection [ l ] . style . background = "red" ;
233
+ var m = Math . floor ( ( l + r ) / 2 ) ;
234
+ await MergeSort ( l , m ) ;
235
+ await MergeSort ( m + 1 , r ) ;
236
+ myCollection [ r ] . style . background = "green" ;
237
+ await merge ( l , m , r ) ;
238
+ myCollection [ l ] . style . background = "cyan" ;
239
+ myCollection [ r ] . style . background = "cyan" ;
240
+ }
241
+ }
242
+
243
+ function doMergeSort ( ) {
244
+ sortSelection = 1 ;
245
+ MergeSort ( 0 , myCollection . length - 1 ) ;
246
+ }
247
+
164
248
165
249
166
250
0 commit comments