Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/sort/bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ function bubble(array) {
var swapped = false;

while (++index < length) {
var current = array[index];
var next = array[index + 1];

if (current > next) {
array[index + 1] = current;
array[index] = next;
var element = array[index];
// compare the adjacent elements
if (element > array[index + 1]) {
// swap elements
array[index] = array[index + 1];
array[index + 1] = element;
swapped = true;
}
}

// if no number was swapped that means
// array is sorted now, break the loop.
if (!swapped) {
break;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/sort/insertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function insertion(array) {
position = position - 1;
}
// insert value at hole position
array[position] = value;
if (position < i) {
array[position] = value;
}
}

return array;
Expand Down
43 changes: 43 additions & 0 deletions lib/sort/selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

/**
* Expose `selection`.
*/

module.exports = selection;

/**
* Selection sort.
*
* @param {Array} array
* @returns {Array}
* @example
*
* selection([2, 3, 1, 5, 4]);
* // => [1, 2, 3, 4, 5]
*
* selection(['b', 'c', 'a', 'e', 'd']);
* // => ['a', 'b', 'c', 'd', 'e']
*/

function selection(array) {
var index = -1;
var length = array.length;

while (++index < length) {
var element = array[index]; // store element
var min = index; // set minimal index
// check the element to be minimum
for (var i = index + 1; i < length; i++) {
if (element > array[i]) {
min = i; // update minimum index
}
}
// swap the minimum element with the current element
if (min > index) {
array[index] = array[min];
array[min] = element;
}
}

return array;
}
7 changes: 5 additions & 2 deletions lib/sort/test/bubble-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var test = require('tape');
var bubble = require('../bubble');

test('bubble(array)', function (t) {
t.deepEqual(bubble([2, 3, 1]), [1, 2, 3], 'should be sorted 1 2 3');
t.deepEqual(bubble(['b', 'c', 'a']), ['a', 'b', 'c'], 'should be sorted a b c');
t.deepEqual(bubble([]), [], 'should return empty array');
t.deepEqual(bubble([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5], 'should return sorted array');
t.deepEqual(bubble([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5], 'should be sorted 1 2 3 4 5');
t.deepEqual(bubble([2, 3, 1, 5, 4]), [1, 2, 3, 4, 5], 'should be sorted 1 2 3 4 5');
t.deepEqual(bubble(['b', 'c', 'a', 'e', 'd']), ['a', 'b', 'c', 'd', 'e'], 'should be sorted a b c d e');
t.end();
});
3 changes: 3 additions & 0 deletions lib/sort/test/insertion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var test = require('tape');
var insertion = require('../insertion');

test('insertion(array)', function (t) {
t.deepEqual(insertion([]), [], 'should return empty array');
t.deepEqual(insertion([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5], 'should return sorted array');
t.deepEqual(insertion([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5], 'should be sorted 1 2 3 4 5');
t.deepEqual(insertion([2, 3, 1, 5, 4]), [1, 2, 3, 4, 5], 'should be sorted 1 2 3 4 5');
t.deepEqual(insertion(['b', 'c', 'a', 'e', 'd']), ['a', 'b', 'c', 'd', 'e'], 'should be sorted a b c d e');
t.end();
Expand Down
11 changes: 11 additions & 0 deletions lib/sort/test/selection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var test = require('tape');
var selection = require('../selection');

test('selection(array)', function (t) {
t.deepEqual(selection([]), [], 'should return empty array');
t.deepEqual(selection([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5], 'should return sorted array');
t.deepEqual(selection([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5], 'should be sorted 1 2 3 4 5');
t.deepEqual(selection([2, 3, 1, 5, 4]), [1, 2, 3, 4, 5], 'should be sorted 1 2 3 4 5');
t.deepEqual(selection(['b', 'c', 'a', 'e', 'd']), ['a', 'b', 'c', 'd', 'e'], 'should be sorted a b c d e');
t.end();
});