<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://unpkg.com/mocha@5.2.0/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha@5.2.0/mocha.js"></script>
<div></div>
</body>
</html>
function getPosition(num, place){
return Math.floor(Math.abs(num)/Math.pow(10,place))% 10
} // gives back bucket index
function getMax(arr){
let max=0;
for(let num of arr){
if(max < num.toString().length){
max = num.toString().length
}
}
return max
}
function radixSort(arr){
const max = getMax(arr);
for(let i=0;i<max;i++){
let buckets = Array.from({length:10},()=>[]) // creating 10 empty arrays
for(let j=0;j<arr.length;j++){
buckets[getPosition(arr[j],i)].push(arr[j]); //push the number into desired
// bucket
}
arr = [].concat(...buckets);
}
return arr
}
console.log(radixSort([4,933,233,3222,1,7,5]))
mocha.setup('bdd');
var assert = chai.assert;
describe('RadixSort',()=>{
it("should be sorted correctly",()=>{
const ans = radixSort([4,933,233,3222,1,7,5])
assert.deepEqual(ans,[1, 4, 5, 7, 233, 933, 3222]);
})
})
mocha.run()
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.