@@ -25,18 +25,45 @@ class Machine {
25
25
26
26
}
27
27
28
+ setPivot ( ) {
29
+
30
+ let avg = ( this . controller . data [ this . start ] + this . controller . data [ this . end ] ) / 2 ;
31
+ let minDelta = abs ( avg - this . controller . data [ this . leftPivot ] ) ;
32
+ let deltaIndex = this . leftPivot ;
33
+
34
+ for ( let i = this . leftPivot ; i < this . end ; i ++ ) {
35
+
36
+ if ( abs ( avg - this . controller . data [ i ] ) < minDelta ) {
37
+
38
+ minDelta = abs ( avg - this . controller . data [ i ] ) ;
39
+ deltaIndex = i ;
40
+
41
+ }
42
+
43
+ }
44
+
45
+ this . controller . swap ( this . leftPivot , deltaIndex ) ;
46
+ return true ;
47
+
48
+ }
49
+
28
50
sendWork ( queueTask ) {
29
51
30
52
this . start = queueTask [ 1 ] ;
31
53
this . end = queueTask [ 2 ] ;
32
54
33
- this . leftPivot = this . start ;
34
- this . rightPivot = this . end ;
55
+ this . leftPivot = this . start + 1 ;
56
+ this . rightPivot = this . end - 1 ;
35
57
36
- this . bestBubbleValue = this . controller . data [ this . end ] ;
37
- this . bestBubbleIndex = this . end ;
58
+ this . bestBubbleValue = this . controller . data [ this . rightPivot ] ;
59
+ this . bestBubbleIndex = this . rightPivot ;
38
60
39
- if ( this . end - this . start > this . controller . zoneLimitSize ) { this . machineStatus = "WORK(JRS)" ; }
61
+ if ( this . end - this . start > this . controller . zoneLimitSize ) {
62
+
63
+ this . setPivot ( ) ;
64
+ this . machineStatus = "WORK(JRS)" ;
65
+
66
+ }
40
67
else { this . machineStatus = "WORK(BBL)" ; }
41
68
42
69
return true ;
@@ -53,8 +80,8 @@ class Machine {
53
80
if ( this . leftPivot == this . rightPivot ) {
54
81
55
82
//Create two queries and go DONE
56
- if ( this . leftPivot - this . start > 1 ) { this . controller . query ( this . leftPivot - this . start , this . start , this . leftPivot ) ; }
57
- if ( this . end - this . leftPivot + 1 > 1 ) { this . controller . query ( this . end - this . leftPivot + 1 , this . leftPivot + 1 , this . end ) ; }
83
+ if ( this . leftPivot - this . start > 3 ) { this . controller . query ( this . leftPivot - this . start , this . start , this . leftPivot ) ; }
84
+ if ( this . end - this . leftPivot + 1 > 3 ) { this . controller . query ( this . end - this . leftPivot , this . leftPivot , this . end ) ; }
58
85
59
86
this . machineStatus = "DONE" ;
60
87
return true ;
@@ -69,8 +96,16 @@ class Machine {
69
96
70
97
if ( this . machineStatus == "WORK(BBL)" ) {
71
98
72
- //Walk downwards and record lowest value
99
+ //Stop when done
100
+ if ( this . leftPivot >= this . end - 1 ) {
101
+
102
+ this . machineStatus = "DONE" ;
103
+ return true ;
104
+
105
+ }
106
+
73
107
this . rightPivot -- ;
108
+ //record lowest value
74
109
if ( this . controller . data [ this . rightPivot ] < this . bestBubbleValue ) {
75
110
76
111
this . bestBubbleValue = this . controller . data [ this . rightPivot ] ;
@@ -79,21 +114,13 @@ class Machine {
79
114
}
80
115
81
116
//Swap when walker reaches the leftPivot
82
- if ( this . rightPivot = = this . leftPivot ) {
117
+ if ( this . rightPivot < = this . leftPivot ) {
83
118
84
- this . controller . swap ( this . rightPivot , this . bestBubbleIndex ) ;
85
- this . bestBubbleValue = this . controller . data [ this . end ] ;
86
- this . bestBubbleIndex = this . end ;
119
+ this . controller . swap ( this . leftPivot , this . bestBubbleIndex ) ;
120
+ this . bestBubbleValue = this . controller . data [ this . end - 1 ] ;
121
+ this . bestBubbleIndex = this . end - 1 ;
87
122
this . leftPivot ++ ;
88
- this . rightPivot = this . end ;
89
- return true ;
90
-
91
- }
92
-
93
- //Stop when done
94
- if ( this . leftPivot == this . end ) {
95
-
96
- this . machineStatus = "DONE" ;
123
+ this . rightPivot = this . end - 1 ;
97
124
return true ;
98
125
99
126
}
0 commit comments