From aad3c4d1510532520c5d6fdb715758938376b3e0 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:23:39 +0530 Subject: [PATCH 1/7] Cocktail Shaker Sort | Improved Description Comment --- Sorts/CocktailShakerSort.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 337fa5f9f..1442fa2a4 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -1,5 +1,8 @@ /* - * Cocktail shaker sort is a sort algorithm that is a bidirectional bubble sort + * Cocktail Shaker Sort is an algorithm that is a Bidirectional Bubble Sort. + * The algorithm extends bubble sort by operating in two directions. + * While it improves on bubble sort by more quickly moving items to the beginning of the list, + * it provides only marginal performance improvements. * more information: https://en.wikipedia.org/wiki/Cocktail_shaker_sort * more information: https://en.wikipedia.org/wiki/Bubble_sort * From 5dbb739fd0b0dbc40675de3ceae3daad147a9064 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:24:09 +0530 Subject: [PATCH 2/7] Cocktail Shaker Sort | Formatted Wikipedia Link --- Sorts/CocktailShakerSort.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 1442fa2a4..1d9c94e38 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -3,8 +3,9 @@ * The algorithm extends bubble sort by operating in two directions. * While it improves on bubble sort by more quickly moving items to the beginning of the list, * it provides only marginal performance improvements. - * more information: https://en.wikipedia.org/wiki/Cocktail_shaker_sort - * more information: https://en.wikipedia.org/wiki/Bubble_sort + * + * Wikipedia (Cocktail Shaker Sort): https://en.wikipedia.org/wiki/Cocktail_shaker_sort + * Wikipedia (Bubble Sort): https://en.wikipedia.org/wiki/Bubble_sort * */ function cocktailShakerSort (items) { From 3a7da23b022a1fcf3638a63c207623b3d9de938b Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:25:09 +0530 Subject: [PATCH 3/7] Cocktail Shaker Sort | Improved Comments --- Sorts/CocktailShakerSort.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 1d9c94e38..ef1ef8fb9 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -13,7 +13,7 @@ function cocktailShakerSort (items) { let swapped = false let j - // backwards + // Backwards for (j = items.length - 1; j > i; j--) { if (items[j] < items[j - 1]) { [items[j], items[j - 1]] = [items[j - 1], items[j]] @@ -21,7 +21,7 @@ function cocktailShakerSort (items) { } } - // forwards + // Forwards for (j = 0; j < i; j++) { if (items[j] > items[j + 1]) { [items[j], items[j + 1]] = [items[j + 1], items[j]] @@ -34,11 +34,12 @@ function cocktailShakerSort (items) { } } -// Implementation of cocktailShakerSort - +/** +* Implementation of Cocktail Shaker Sort +*/ var ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort +// Before Sort console.log(ar) cocktailShakerSort(ar) -// Array after sort +// After Sort console.log(ar) From ac488fe6267e01082845f312a5b64cc371d847e6 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:25:59 +0530 Subject: [PATCH 4/7] Cocktail Shaker Sort | Code wasn't returning sorted Items. Changed that. --- Sorts/CocktailShakerSort.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index ef1ef8fb9..8053175ef 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -10,7 +10,6 @@ */ function cocktailShakerSort (items) { for (let i = items.length - 1; i > 0; i--) { - let swapped = false let j // Backwards @@ -28,10 +27,9 @@ function cocktailShakerSort (items) { swapped = true } } - if (!swapped) { - return - } } + + return items } /** From aab8a555fcd2818005ca724e37e82207f87d90dd Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:26:21 +0530 Subject: [PATCH 5/7] Cocktail Shaker Sort | Improved Implementation --- Sorts/CocktailShakerSort.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 8053175ef..f32a1f2ad 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -35,9 +35,11 @@ function cocktailShakerSort (items) { /** * Implementation of Cocktail Shaker Sort */ -var ar = [5, 6, 7, 8, 1, 2, 12, 14] +var array = [5, 6, 7, 8, 1, 2, 12, 14] // Before Sort -console.log(ar) -cocktailShakerSort(ar) +console.log('\n- Before Sort | Implementation of Cocktail Shaker Sort -') +console.log(array) // After Sort -console.log(ar) +console.log('- After Sort | Implementation of Cocktail Shaker Sort -') +console.log(cocktailShakerSort(array)) +console.log('\n') From 8d1278b4559e1fe460d7aae3d20ddec460b61910 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:26:47 +0530 Subject: [PATCH 6/7] Cocktail Shaker Sort | Added Doctests --- Sorts/CocktailShakerSort.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index f32a1f2ad..a6e13548f 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -8,6 +8,18 @@ * Wikipedia (Bubble Sort): https://en.wikipedia.org/wiki/Bubble_sort * */ + + /** + * Doctests + * + * > cocktailShakerSort([5, 4, 1, 2, 3]) + * [1, 2, 3, 4, 5] + * > cocktailShakerSort([]) + * [] + * > cocktailShakerSort([1, 2, 3]) + * [1, 2, 3] + */ + function cocktailShakerSort (items) { for (let i = items.length - 1; i > 0; i--) { let j From d44ffef467a249bf53a48c73952ca93e178cf3be Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 2 Nov 2020 15:32:45 +0530 Subject: [PATCH 7/7] Cocktail Shaker Sort | Fixed 'standard' warnings and errors --- Sorts/CocktailShakerSort.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index a6e13548f..cabdcb0a0 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -9,7 +9,7 @@ * */ - /** +/** * Doctests * * > cocktailShakerSort([5, 4, 1, 2, 3]) @@ -28,7 +28,6 @@ function cocktailShakerSort (items) { for (j = items.length - 1; j > i; j--) { if (items[j] < items[j - 1]) { [items[j], items[j - 1]] = [items[j - 1], items[j]] - swapped = true } } @@ -36,7 +35,6 @@ function cocktailShakerSort (items) { for (j = 0; j < i; j++) { if (items[j] > items[j + 1]) { [items[j], items[j + 1]] = [items[j + 1], items[j]] - swapped = true } } } @@ -47,7 +45,7 @@ function cocktailShakerSort (items) { /** * Implementation of Cocktail Shaker Sort */ -var array = [5, 6, 7, 8, 1, 2, 12, 14] +const array = [5, 6, 7, 8, 1, 2, 12, 14] // Before Sort console.log('\n- Before Sort | Implementation of Cocktail Shaker Sort -') console.log(array)