Skip to content

Commit 5af6d72

Browse files
committed
added bucket and counting sort
1 parent 2aa102f commit 5af6d72

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

chapter10/01-SortingSearchingAlgorithms.js

+73-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ function ArrayList(){
66
array.push(item);
77
};
88

9-
var swap = function(index1, index2){
9+
var swap = function(array, index1, index2){
1010
var aux = array[index1];
1111
array[index1] = array[index2];
1212
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]];
1315
};
1416

1517
this.toString= function(){
@@ -25,7 +27,7 @@ function ArrayList(){
2527
console.log('compare ' + array[j] + ' with ' + array[j+1]);
2628
if (array[j] > array[j+1]){
2729
console.log('swap ' + array[j] + ' with ' + array[j+1]);
28-
swap(j, j+1);
30+
swap(array, j, j+1);
2931
}
3032
}
3133
}
@@ -84,6 +86,20 @@ function ArrayList(){
8486
}
8587
};
8688

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+
87103
this.mergeSort = function(){
88104
array = mergeSortRec(array);
89105
};
@@ -156,7 +172,7 @@ function ArrayList(){
156172

157173
if (i <= j) {
158174
console.log('swap ' + array[i] + ' with ' + array[j]);
159-
swapQuickStort(array, i, j);
175+
swap(array, i, j);
160176
i++;
161177
j--;
162178
}
@@ -165,12 +181,6 @@ function ArrayList(){
165181
return i;
166182
};
167183

168-
var swapQuickStort = function(array, index1, index2){
169-
var aux = array[index1];
170-
array[index1] = array[index2];
171-
array[index2] = aux;
172-
};
173-
174184
var quick = function(array, left, right){
175185

176186
var index;
@@ -190,6 +200,60 @@ function ArrayList(){
190200
return array;
191201
};
192202

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+
193257
this.sequentialSearch = function(item){
194258

195259
for (var i=0; i<array.length; i++){

0 commit comments

Comments
 (0)