Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathbucketSort.js
More file actions
Latest commit
46 lines (46 loc) · 1.18 KB
/
Copy pathbucketSort.js
File metadata and controls
46 lines (46 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
constarr=[32,6,34,4,78,1,6767,4,65,34,879,7];
constbucketSort=arr=>{
if(arr.length===0){
returnarr;
}
leti,
minValue=arr[0],
maxValue=arr[0],
bucketSize=5;
arr.forEach(function(currentVal){
if(currentVal<minValue){
minValue=currentVal;
}elseif(currentVal>maxValue){
maxValue=currentVal;
}
})
letbucketCount=Math.floor((maxValue-minValue)/bucketSize)+1;
letallBuckets=newArray(bucketCount);
for(i=0;i<allBuckets.length;i++){
allBuckets[i]=[];
}
arr.forEach(function(currentVal){
allBuckets[Math.floor((currentVal-minValue)/bucketSize)].push(currentVal);
});
arr.length=0;
allBuckets.forEach(function(bucket){
insertion(bucket);
bucket.forEach(function(element){
arr.push(element)
});
});
returnarr;
}
constinsertion=arr=>{
letlength=arr.length;
leti,j;
for(i=1;i<length;i++){
lettemp=arr[i];
for(j=i-1;j>=0&&arr[j]>temp;j--){
arr[j+1]=arr[j];
}
arr[j+1]=temp;
}
returnarr;
};
console.log(bucketSort(arr));