From 5c09db37479027f36c691035c57a1a2568858972 Mon Sep 17 00:00:00 2001 From: Anton Yefremov Date: Mon, 6 Feb 2017 00:32:14 +0200 Subject: [PATCH 1/3] Add explanation comments --- lib/sort/bubble.js | 15 ++++++++------- lib/sort/insertion.js | 4 +++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/sort/bubble.js b/lib/sort/bubble.js index b759bd5..7d9da2a 100644 --- a/lib/sort/bubble.js +++ b/lib/sort/bubble.js @@ -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; } diff --git a/lib/sort/insertion.js b/lib/sort/insertion.js index 545be4c..f33388a 100644 --- a/lib/sort/insertion.js +++ b/lib/sort/insertion.js @@ -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; From 1d77a38a5f22b85c6c821e00f5b99f651253a96a Mon Sep 17 00:00:00 2001 From: Anton Yefremov Date: Mon, 6 Feb 2017 00:32:39 +0200 Subject: [PATCH 2/3] Add sort algorithms test cases --- lib/sort/test/bubble-test.js | 7 +++++-- lib/sort/test/insertion-test.js | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/sort/test/bubble-test.js b/lib/sort/test/bubble-test.js index 02ef748..e3d44fe 100644 --- a/lib/sort/test/bubble-test.js +++ b/lib/sort/test/bubble-test.js @@ -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(); }); diff --git a/lib/sort/test/insertion-test.js b/lib/sort/test/insertion-test.js index 2bbb181..c0ce25a 100644 --- a/lib/sort/test/insertion-test.js +++ b/lib/sort/test/insertion-test.js @@ -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(); From 11517dabd7d285193fb226aca5fc29eef4231196 Mon Sep 17 00:00:00 2001 From: Anton Yefremov Date: Mon, 6 Feb 2017 00:32:56 +0200 Subject: [PATCH 3/3] Implement selection sort --- lib/sort/selection.js | 43 +++++++++++++++++++++++++++++++++ lib/sort/test/selection-test.js | 11 +++++++++ 2 files changed, 54 insertions(+) create mode 100644 lib/sort/selection.js create mode 100644 lib/sort/test/selection-test.js diff --git a/lib/sort/selection.js b/lib/sort/selection.js new file mode 100644 index 0000000..b7bb7cf --- /dev/null +++ b/lib/sort/selection.js @@ -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; +} diff --git a/lib/sort/test/selection-test.js b/lib/sort/test/selection-test.js new file mode 100644 index 0000000..58b995d --- /dev/null +++ b/lib/sort/test/selection-test.js @@ -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(); +});