Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3da6df7

Browse files
committed
Changes in bucketsort
1 parent91d8a60 commit3da6df7

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
functioncreateBuckets(array){
4545
varbuckets=[],
46-
length=array.length,
4746
currentBucket,current;
4847
for(vari=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(vari=0;i<buckets.length;i+=1){
8584
currentBucket=buckets[i];
8685
if(currentBucket!==undefined){
87-
for(varj=0;j<currentBucket.length;j+=1){
88-
result.push(currentBucket[j]);
89-
}
86+
result=result.concat(currentBucket);
9087
}
9188
}
9289
returnresult;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
varsortTestCase=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-
functioncreateRandomArray(options){
8-
options=options||{};
9-
varsize=options.size||100,
10-
precision=options.precision||2,
11-
multiplier=options.multiplier||100;
12+
functioncreateRandomArray(config){
13+
config=config||{};
14+
varsize=config.size||100,
15+
precision=config.precision||2,
16+
multiplier=config.multiplier||100;
1217

1318
varresult=[];
1419
for(vari=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-
vararray=createRandomArray();
35+
vararray;
36+
if(options.integers){
37+
array=createRandomArray();
38+
}else{
39+
array=createRandomArray({
40+
precision:0
41+
});
42+
}
3243
sort(array);
33-
3444
for(vari=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-
functioncomparator(a,b){
42-
returnb-a;
43-
}
49+
if(options.reverse){
50+
it('should sort the numbers in descending order '+
51+
'when such comparator is provided',function(){
52+
functioncomparator(a,b){
53+
returnb-a;
54+
}
4455

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

48-
for(vari=0;i<array.length-1;i+=1){
49-
expect(array[i]>=array[i+1]).toBeTruthy();
50-
}
51-
});
59+
for(vari=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)

[8]ページ先頭

©2009-2025 Movatter.jp