Skip to content

Commit 3da6df7

Browse files
committed
Changes in bucketsort
1 parent 91d8a60 commit 3da6df7

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

src/sorting/linearsort/bucketsort.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343
*/
4444
function createBuckets(array) {
4545
var buckets = [],
46-
length = array.length,
4746
currentBucket, current;
4847
for (var i = 0; i < array.length; i += 1) {
4948
current = array[i];
50-
currentBucket = Math.floor(length % current);
49+
currentBucket = Math.floor(current);
5150
buckets[currentBucket] = buckets[currentBucket] || [];
5251
buckets[currentBucket].push(current);
5352
}
@@ -84,9 +83,7 @@
8483
for (var i = 0; i < buckets.length; i += 1) {
8584
currentBucket = buckets[i];
8685
if (currentBucket !== undefined) {
87-
for (var j = 0; j < currentBucket.length; j += 1) {
88-
result.push(currentBucket[j]);
89-
}
86+
result = result.concat(currentBucket);
9087
}
9188
}
9289
return result;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var sortTestCase = require('../sort.testcase.js'),
2-
shellSort = require('../../../src/sorting/shellsort/shellsort.js').shellSort;
2+
shellSort = require('../../../src/sorting/shellsort/shellsort.js')
3+
.shellSort;
34

45
sortTestCase(shellSort, 'Shell sort');

test/sorting/sort.testcase.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
module.exports = function (sort, algorithmName) {
1+
module.exports = function (sort, algorithmName, options) {
22

33
'use strict';
44

5+
options = options || {
6+
integers: false,
7+
reverse : true
8+
};
9+
510
describe(algorithmName, function () {
611

7-
function createRandomArray(options) {
8-
options = options || {};
9-
var size = options.size || 100,
10-
precision = options.precision || 2,
11-
multiplier = options.multiplier || 100;
12+
function createRandomArray(config) {
13+
config = config || {};
14+
var size = config.size || 100,
15+
precision = config.precision || 2,
16+
multiplier = config.multiplier || 100;
1217

1318
var result = [];
1419
for (var i = size; i > 0; i -= 1) {
@@ -27,28 +32,35 @@ module.exports = function (sort, algorithmName) {
2732
});
2833

2934
it('should work with random non-sorted arrays', function () {
30-
31-
var array = createRandomArray();
35+
var array;
36+
if (options.integers) {
37+
array = createRandomArray();
38+
} else {
39+
array = createRandomArray({
40+
precision: 0
41+
});
42+
}
3243
sort(array);
33-
3444
for (var i = 0; i < array.length - 1; i += 1) {
3545
expect(array[i] <= array[i + 1]).toBeTruthy();
3646
}
3747
});
3848

39-
it('should sort the numbers in descending order ' +
40-
'when such comparator is provided', function () {
41-
function comparator(a, b) {
42-
return b - a;
43-
}
49+
if (options.reverse) {
50+
it('should sort the numbers in descending order ' +
51+
'when such comparator is provided', function () {
52+
function comparator(a, b) {
53+
return b - a;
54+
}
4455

45-
var array = createRandomArray();
46-
sort(array, comparator);
56+
var array = createRandomArray();
57+
sort(array, comparator);
4758

48-
for (var i = 0; i < array.length - 1; i += 1) {
49-
expect(array[i] >= array[i + 1]).toBeTruthy();
50-
}
51-
});
59+
for (var i = 0; i < array.length - 1; i += 1) {
60+
expect(array[i] >= array[i + 1]).toBeTruthy();
61+
}
62+
});
63+
}
5264

5365
});
5466
};

0 commit comments

Comments
 (0)