Skip to content

Commit

Permalink
Add bubble sort
Browse files Browse the repository at this point in the history
  • Loading branch information
terryx committed Oct 26, 2016
1 parent 42b9110 commit d147bff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sorting/bubble.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const bubble = (nums) => {
let done;

do {
done = true;

for (let i = 0; i < nums.length; i++) {
if (nums[i] > nums[i + 1]) {
const temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
done = false
}
}

} while(!done);

return nums;
}

module.exports = bubble
8 changes: 8 additions & 0 deletions tests/bubble.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const test = require('ava');
const bubble = require('../sorting/bubble');

test('bubble sort', t => {
const nums = [1,2,3,6,7,4,9,8,19,13]

t.deepEqual(bubble(nums), [1,2,3,4,6,7,8,9,13,19])
})

0 comments on commit d147bff

Please sign in to comment.