From 358ff658185ebea8315e296c74e7e9c936b44ab9 Mon Sep 17 00:00:00 2001 From: George Kats Date: Sun, 23 Jun 2013 12:42:35 +0300 Subject: [PATCH 1/3] Add shell sort algorithm. --- algorithms/sorting/shell-sort/shell-sort.js | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 algorithms/sorting/shell-sort/shell-sort.js diff --git a/algorithms/sorting/shell-sort/shell-sort.js b/algorithms/sorting/shell-sort/shell-sort.js new file mode 100644 index 0000000..2fa7a31 --- /dev/null +++ b/algorithms/sorting/shell-sort/shell-sort.js @@ -0,0 +1,38 @@ +/** + * Swaps two values in an array. + * @param {Array} items The array containing the items. + * @param {int} firstIndex Index of first item to swap. + * @param {int} secondIndex Index of second item to swap. + * @return {void} + */ +function swap(items, firstIndex, secondIndex){ + var temp = items[firstIndex]; + items[firstIndex] = items[secondIndex]; + items[secondIndex] = temp; +} + +/** + * A shell sort implementation in JavaScript. + * @param {Array} items An array of items to sort. + * @return {Array} The sorted array. + */ +function shellSort(items) { + var step = items.length, + i, j; + + do { + step = Math.floor(step/2); + i = 0; + j = i + step; + while (j < arr.length) { + if (items[i] > items[j]) { + swap(items, i, j); + } + + i++; + j = i + step; + } + } while (step >= 1); + + return items; +} From 3ff29df0c866f9e7539a4dcffd1ab4d45001b716 Mon Sep 17 00:00:00 2001 From: George Kats Date: Sun, 23 Jun 2013 20:32:41 +0300 Subject: [PATCH 2/3] Fix shell sort. --- algorithms/sorting/shell-sort/shell-sort.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/algorithms/sorting/shell-sort/shell-sort.js b/algorithms/sorting/shell-sort/shell-sort.js index 2fa7a31..d80554e 100644 --- a/algorithms/sorting/shell-sort/shell-sort.js +++ b/algorithms/sorting/shell-sort/shell-sort.js @@ -17,14 +17,15 @@ function swap(items, firstIndex, secondIndex){ * @return {Array} The sorted array. */ function shellSort(items) { - var step = items.length, - i, j; + var len = items.length + step = len, + i, j; do { step = Math.floor(step/2); i = 0; j = i + step; - while (j < arr.length) { + while (j < len) { if (items[i] > items[j]) { swap(items, i, j); } From 8ea3d62d8396744a2745229eb16d65bec61e1970 Mon Sep 17 00:00:00 2001 From: George Kats Date: Sun, 23 Jun 2013 13:14:08 +0300 Subject: [PATCH 3/3] Add binary insertion sort. --- .../insertion-sort/binary-insertion-sort.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 algorithms/sorting/insertion-sort/binary-insertion-sort.js diff --git a/algorithms/sorting/insertion-sort/binary-insertion-sort.js b/algorithms/sorting/insertion-sort/binary-insertion-sort.js new file mode 100644 index 0000000..b09ee17 --- /dev/null +++ b/algorithms/sorting/insertion-sort/binary-insertion-sort.js @@ -0,0 +1,30 @@ +/** + * An binary insertion sort implementation in JavaScript. + * @param {Array} items An array of items to sort. + * @return {Array} The sorted array. + */ + +function binaryInsertionSort(items) { + var len = items.length, + left, right, center, i, item; + + for (i = 1; i < len; i++) { + left = 0; + right = i - 1; + + while (left < right) { + center = Math.floor((left + right) / 2); + if (items[center] > items[i]) { + right = center; + } else { + left = center + 1; + } + } + + // Remove element from array and paste to new position + item = items.splice(i, 1)[0]; + items.splice(left, 0, item); + } + + return items; +}