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 a5686da58e3ea6cbde1b03f368eb0b70fa760e74 Mon Sep 17 00:00:00 2001 From: George Kats Date: Sun, 23 Jun 2013 12:49:36 +0300 Subject: [PATCH 3/3] Add shaker sort. --- algorithms/sorting/shaker-sort/shaker-sort.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 algorithms/sorting/shaker-sort/shaker-sort.js diff --git a/algorithms/sorting/shaker-sort/shaker-sort.js b/algorithms/sorting/shaker-sort/shaker-sort.js new file mode 100644 index 0000000..5347028 --- /dev/null +++ b/algorithms/sorting/shaker-sort/shaker-sort.js @@ -0,0 +1,41 @@ +/** + * 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 shaker sort implementation in JavaScript. + * @param {Array} items An array of items to sort. + * @return {Array} The sorted array. + */ +function shakerSort(items) { + var start = 0, + end = items.length - 1, + i; + + do { + + for (i = end; i > 0; i--) { + if (items[i-1] >= items[i]) { + swap(items, i, i-1); + start = i + 1; + } + } + + for (i = 1; i <= start; i++) { + if (items[i-1] > items[i]) { + swap(items, i, i-1); + end = i - 1; + } + } + + } while (start < end); +}