@@ -3,74 +3,66 @@ tracer._sleep(1000);
33tracer . _pace ( 500 ) ;
44
55function mergeSort ( start , end ) {
6- if ( Math . abs ( end - start ) <= 1 ) {
7- return [ ] ;
8- }
9-
6+ if ( Math . abs ( end - start ) <= 1 ) return [ ] ;
107 var middle = Math . ceil ( ( start + end ) / 2 ) ;
118
129 mergeSort ( start , middle ) ;
1310 mergeSort ( middle , end ) ;
14-
15- tracer . _print ( 'divide left[' + start + ', ' + ( middle - 1 ) + '], right[' + ( middle ) + ', ' + ( end - 1 ) + ']' ) ;
1611
12+ tracer . _print ( 'divide left[' + start + ', ' + ( middle - 1 ) + '], right[' + ( middle ) + ', ' + ( end - 1 ) + ']' ) ;
1713 return mergeSort . merge ( start , middle , end ) ;
1814}
1915
20- mergeSort . merge = function ( start , middle , end ) {
21- var left = Array ( ) ;
22- var right = Array ( ) ;
23-
24- var leftSize = middle - start ;
25- var rightSize = end - middle ;
26- var maxSize = Math . max ( leftSize , rightSize ) ;
27- var size = end - start ;
16+ mergeSort . merge = function ( start , middle , end ) {
17+ const leftSize = middle - start ;
18+ const rightSize = end - middle ;
19+ const maxSize = Math . max ( leftSize , rightSize ) ;
20+ const size = end - start ;
21+ var left = [ ] ;
22+ var right = [ ] ;
2823 var i ;
2924
3025 for ( i = 0 ; i < maxSize ; i ++ ) {
31- if ( i < leftSize ) {
32- left . push ( D [ start + i ] ) ;
33- tracer . _select ( start + i ) ;
34- tracer . _print ( 'insert value into left array[' + i + '] = ' + D [ start + i ] ) ;
35- }
36- if ( i < rightSize ) {
37- right . push ( D [ middle + i ] ) ;
38- tracer . _select ( middle + i ) ;
39- tracer . _print ( 'insert value into right array[' + i + '] = ' + D [ middle + i ] ) ;
40- }
26+ if ( i < leftSize ) {
27+ left . push ( D [ start + i ] ) ;
28+ tracer . _select ( start + i ) ;
29+ tracer . _print ( 'insert value into left array[' + i + '] = ' + D [ start + i ] ) ;
30+ }
31+ if ( i < rightSize ) {
32+ right . push ( D [ middle + i ] ) ;
33+ tracer . _select ( middle + i ) ;
34+ tracer . _print ( 'insert value into right array[' + i + '] = ' + D [ middle + i ] ) ;
35+ }
4136 }
4237 tracer . _print ( 'left array = [' + left . join ( ', ' ) + '],' + 'right array = [' + right . join ( ', ' ) + ']' ) ;
43-
38+
4439 i = 0 ;
4540 while ( i < size ) {
46- if ( left [ 0 ] && right [ 0 ] ) {
47- if ( left [ 0 ] > right [ 0 ] ) {
48- D [ start + i ] = right . shift ( ) ;
49- tracer . _print ( 'rewrite from right array[' + i + '] = ' + D [ start + i ] ) ;
41+ if ( left [ 0 ] && right [ 0 ] ) {
42+ if ( left [ 0 ] > right [ 0 ] ) {
43+ D [ start + i ] = right . shift ( ) ;
44+ tracer . _print ( 'rewrite from right array[' + i + '] = ' + D [ start + i ] ) ;
45+ } else {
46+ D [ start + i ] = left . shift ( ) ;
47+ tracer . _print ( 'rewrite from left array[' + i + '] = ' + D [ start + i ] ) ;
48+ }
49+ } else if ( left [ 0 ] ) {
50+ D [ start + i ] = left . shift ( ) ;
51+ tracer . _print ( 'rewrite from left array[' + i + '] = ' + D [ start + i ] ) ;
5052 } else {
51- D [ start + i ] = left . shift ( ) ;
52- tracer . _print ( 'rewrite from left array[' + i + '] = ' + D [ start + i ] ) ;
53+ D [ start + i ] = right . shift ( ) ;
54+ tracer . _print ( 'rewrite from right array[' + i + '] = ' + D [ start + i ] ) ;
5355 }
54- } else if ( left [ 0 ] ) {
55- D [ start + i ] = left . shift ( ) ;
56- tracer . _print ( 'rewrite from left array[' + i + '] = ' + D [ start + i ] ) ;
57- } else {
58- D [ start + i ] = right . shift ( ) ;
59- tracer . _print ( 'rewrite from right array[' + i + '] = ' + D [ start + i ] ) ;
60- }
61-
62- tracer . _deselect ( start + i ) ;
63- tracer . _notify ( start + i ) ;
64-
65- i ++ ;
66- }
67-
68- tempArray = Array ( ) ;
69- for ( i = start ; i < end ; i ++ ) {
70- tempArray . push ( D [ i ] ) ;
56+
57+ tracer . _deselect ( start + i ) ;
58+ tracer . _notify ( start + i ) ;
59+ i ++ ;
7160 }
61+
62+ tempArray = [ ] ;
63+ for ( i = start ; i < end ; i ++ ) tempArray . push ( D [ i ] ) ;
7264 tracer . _print ( 'merged array = [' + tempArray . join ( ', ' ) + ']' ) ;
73- }
65+ } ;
7466
75- mergeSort ( 0 , D . length )
67+ mergeSort ( 0 , D . length ) ;
7668tracer . _print ( 'sorted array = [' + D . join ( ', ' ) + ']' ) ;
0 commit comments