@@ -6,10 +6,12 @@ function ArrayList(){
6
6
array . push ( item ) ;
7
7
} ;
8
8
9
- var swap = function ( index1 , index2 ) {
9
+ var swap = function ( array , index1 , index2 ) {
10
10
var aux = array [ index1 ] ;
11
11
array [ index1 ] = array [ index2 ] ;
12
12
array [ index2 ] = aux ;
13
+ //ES2015 swap - Firefox only, for other browser, uncomment code above and coment line below
14
+ //[array[index1], array[index2]] = [array[index2], array[index1]];
13
15
} ;
14
16
15
17
this . toString = function ( ) {
@@ -25,7 +27,7 @@ function ArrayList(){
25
27
console . log ( 'compare ' + array [ j ] + ' with ' + array [ j + 1 ] ) ;
26
28
if ( array [ j ] > array [ j + 1 ] ) {
27
29
console . log ( 'swap ' + array [ j ] + ' with ' + array [ j + 1 ] ) ;
28
- swap ( j , j + 1 ) ;
30
+ swap ( array , j , j + 1 ) ;
29
31
}
30
32
}
31
33
}
@@ -84,6 +86,20 @@ function ArrayList(){
84
86
}
85
87
} ;
86
88
89
+ var insertionSort_ = function ( array ) {
90
+ var length = array . length ,
91
+ j , temp ;
92
+ for ( var i = 1 ; i < length ; i ++ ) {
93
+ j = i ;
94
+ temp = array [ i ] ;
95
+ while ( j > 0 && array [ j - 1 ] > temp ) {
96
+ array [ j ] = array [ j - 1 ] ;
97
+ j -- ;
98
+ }
99
+ array [ j ] = temp ;
100
+ }
101
+ } ;
102
+
87
103
this . mergeSort = function ( ) {
88
104
array = mergeSortRec ( array ) ;
89
105
} ;
@@ -156,7 +172,7 @@ function ArrayList(){
156
172
157
173
if ( i <= j ) {
158
174
console . log ( 'swap ' + array [ i ] + ' with ' + array [ j ] ) ;
159
- swapQuickStort ( array , i , j ) ;
175
+ swap ( array , i , j ) ;
160
176
i ++ ;
161
177
j -- ;
162
178
}
@@ -165,12 +181,6 @@ function ArrayList(){
165
181
return i ;
166
182
} ;
167
183
168
- var swapQuickStort = function ( array , index1 , index2 ) {
169
- var aux = array [ index1 ] ;
170
- array [ index1 ] = array [ index2 ] ;
171
- array [ index2 ] = aux ;
172
- } ;
173
-
174
184
var quick = function ( array , left , right ) {
175
185
176
186
var index ;
@@ -190,6 +200,60 @@ function ArrayList(){
190
200
return array ;
191
201
} ;
192
202
203
+ this . countingSort = function ( ) {
204
+
205
+ var i ,
206
+ maxValue = this . findMaxValue ( ) ,
207
+ sortedIndex = 0 ,
208
+ counts = new Array ( maxValue + 1 ) ;
209
+
210
+ for ( i = 0 ; i < array . length ; i ++ ) {
211
+ if ( ! counts [ array [ i ] ] ) {
212
+ counts [ array [ i ] ] = 0 ;
213
+ }
214
+ counts [ array [ i ] ] ++ ;
215
+ }
216
+
217
+ console . log ( 'Frequencies: ' + counts . join ( ) ) ;
218
+
219
+ for ( i = 0 ; i < counts . length ; i ++ ) {
220
+ while ( counts [ i ] > 0 ) {
221
+ array [ sortedIndex ++ ] = i ;
222
+ counts [ i ] -- ;
223
+ }
224
+ }
225
+ } ;
226
+
227
+ this . bucketSort = function ( bucketSize ) {
228
+
229
+ var i ,
230
+ minValue = this . findMinValue ( ) ,
231
+ maxValue = this . findMaxValue ( ) ,
232
+ BUCKET_SIZE = 10 ;
233
+
234
+ bucketSize = bucketSize || BUCKET_SIZE ;
235
+ var bucketCount = Math . floor ( ( maxValue - minValue ) / bucketSize ) + 1 ;
236
+ var buckets = new Array ( bucketCount ) ;
237
+ for ( i = 0 ; i < buckets . length ; i ++ ) {
238
+ buckets [ i ] = [ ] ;
239
+ }
240
+
241
+ for ( i = 0 ; i < array . length ; i ++ ) {
242
+ buckets [ Math . floor ( ( array [ i ] - minValue ) / bucketSize ) ] . push ( array [ i ] ) ;
243
+ }
244
+
245
+ array = [ ] ;
246
+ for ( i = 0 ; i < buckets . length ; i ++ ) {
247
+ insertionSort_ ( buckets [ i ] ) ;
248
+
249
+ console . log ( 'bucket ' + i + ': ' + buckets [ i ] . join ( ) ) ;
250
+
251
+ for ( var j = 0 ; j < buckets [ i ] . length ; j ++ ) {
252
+ array . push ( buckets [ i ] [ j ] ) ;
253
+ }
254
+ }
255
+ } ;
256
+
193
257
this . sequentialSearch = function ( item ) {
194
258
195
259
for ( var i = 0 ; i < array . length ; i ++ ) {
0 commit comments