@@ -102,7 +102,7 @@ var jssort = jssort || {};
102
102
103
103
if ( lo >= hi ) return ;
104
104
105
- if ( hi - lo > = 7 ) {
105
+ if ( hi - lo < = 7 ) {
106
106
jss . insertionSort ( a , lo , hi , compare ) ;
107
107
return ;
108
108
}
@@ -134,6 +134,55 @@ var jssort = jssort || {};
134
134
}
135
135
}
136
136
} ;
137
+
138
+ jss . quickSort = function ( a , lo , hi , compare ) {
139
+ if ( ! lo ) lo = 0 ;
140
+ if ( ! hi ) hi = a . length - 1 ;
141
+ if ( ! compare ) {
142
+ compare = function ( a1 , a2 ) {
143
+ return a1 - a2 ;
144
+ } ;
145
+ }
146
+
147
+ if ( lo >= hi ) {
148
+ return ;
149
+ }
150
+
151
+ if ( hi - lo <= 7 ) {
152
+ jss . insertionSort ( a , lo , hi , compare ) ;
153
+ return ;
154
+ }
155
+
156
+ var j = jss . partition ( a , lo , hi , compare ) ;
157
+ jss . quickSort ( a , lo , j - 1 , compare ) ;
158
+ jss . quickSort ( a , j + 1 , hi , compare ) ;
159
+ } ;
160
+
161
+ jss . partition = function ( a , lo , hi , compare ) {
162
+ var v = a [ lo ] ;
163
+ var i = lo , j = hi + 1 ;
164
+ while ( true ) {
165
+ while ( jss . less ( a [ ++ i ] , v , compare ) ) {
166
+ if ( i >= hi ) {
167
+ break ;
168
+ }
169
+ }
170
+ while ( jss . less ( v , a [ -- j ] , compare ) ) {
171
+ if ( j <= lo ) {
172
+ break ;
173
+ }
174
+ }
175
+
176
+ if ( i >= j ) {
177
+ break ;
178
+ }
179
+
180
+ jss . exchange ( a , i , j ) ;
181
+ }
182
+
183
+ jss . exchange ( a , lo , j ) ;
184
+ return j ;
185
+ } ;
137
186
138
187
} ) ( jssort ) ;
139
188
0 commit comments