From 95929c4552a3791cbe4c28f73186efb1296ff869 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:27:14 +0530 Subject: [PATCH 01/11] BubbleSort | Improved Comments and Formatting --- Sorts/BubbleSort.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 3232991d1d..4081e60bb8 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -1,11 +1,20 @@ -/* Bubble Sort is a algorithm to sort an array. It -* compares adjacent element and swaps thier position -* The big O on bubble sort in worst and best case is O(N^2). - * Not efficient. +/* Bubble Sort is an algorithm to sort an array. It +* compares adjacent element and swaps thier position +* The big O on bubble sort in worst and best case is O(N^2). +* Not efficient. +* +* In bubble sort, we keep iterating while something was swapped in +* the previous inner-loop iteration. By swapped I mean, in the +* inner loop iteration, we check each number if the number proceeding +* it is greater than itself, if so we swap them. */ -function bubbleSort (items) { +/* +* Using 2 for loops +*/ +function bubbleSort(items) { const length = items.length + for (let i = (length - 1); i > 0; i--) { // Number of passes for (let j = (length - i); j > 0; j--) { @@ -16,10 +25,12 @@ function bubbleSort (items) { } } } -} -// Implementation of bubbleSort +} +/* +* Implementation of 2 for loops method +*/ var ar = [5, 6, 7, 8, 1, 2, 12, 14] // Array before Sort console.log('-----before sorting-----') @@ -29,18 +40,12 @@ bubbleSort(ar) console.log('-----after sorting-----') console.log(ar) -/* alternative implementation of bubble sort algorithm. - Using a while loop instead. For educational purposses only - */ /* -*In bubble sort, we keep iterating while something was swapped in -*the previous inner-loop iteration. By swapped I mean, in the -*inner loop iteration, we check each number if the number proceeding -*it is greater than itself, if so we swap them. +* Using a while loop and a for loop */ - -function alternativeBubbleSort (arr) { +function alternativeBubbleSort(arr) { let swapped = true + while (swapped) { swapped = false for (let i = 0; i < arr.length - 1; i++) { @@ -50,10 +55,13 @@ function alternativeBubbleSort (arr) { } } } + return arr } -// test +/* +* Implementation of a while loop and a for loop method +*/ console.log('-----before sorting-----') var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] console.log(array) From ec171a9aea556cc2665d9c57ac43a516e7bb343e Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:29:13 +0530 Subject: [PATCH 02/11] BubbleSort | Returning the items in the first BubbleSort function --- Sorts/BubbleSort.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 4081e60bb8..68cafdf5bc 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -26,6 +26,7 @@ function bubbleSort(items) { } } + return items } /* @@ -35,10 +36,9 @@ var ar = [5, 6, 7, 8, 1, 2, 12, 14] // Array before Sort console.log('-----before sorting-----') console.log(ar) -bubbleSort(ar) // Array after sort console.log('-----after sorting-----') -console.log(ar) +console.log(bubbleSort(ar)) /* * Using a while loop and a for loop From ce8ce4d5c195fa6d9c1a8d436294ce1a459df967 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:32:51 +0530 Subject: [PATCH 03/11] BubbleSort | Improved the Implementation and Console Logs --- Sorts/BubbleSort.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 68cafdf5bc..296d4a49c4 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -32,13 +32,16 @@ function bubbleSort(items) { /* * Implementation of 2 for loops method */ -var ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log('-----before sorting-----') -console.log(ar) -// Array after sort -console.log('-----after sorting-----') -console.log(bubbleSort(ar)) +var array1 = [5, 6, 7, 8, 1, 2, 12, 14] +// Before Sort +console.log('- Before Sort | Implementation using 2 for loops -') +console.log(array1) +// After Sort +console.log('- After Sort | Implementation using 2 for loops -') +console.log(bubbleSort(array1)) + +/* Separating Console Logs */ +console.log("\n\n") /* * Using a while loop and a for loop @@ -62,8 +65,10 @@ function alternativeBubbleSort(arr) { /* * Implementation of a while loop and a for loop method */ -console.log('-----before sorting-----') -var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] -console.log(array) -console.log('-----after sorting-----') -console.log(alternativeBubbleSort(array)) +var array2 = [5, 6, 7, 8, 1, 2, 12, 14] +// Before Sort +console.log('- Before Sort | Implementation using a while loop and a for loop -') +console.log(array2) +// After Sort +console.log('- After Sort | Implementation using a while loop and a for loop -') +console.log(bubbleSort(array2)) From 395df1c5b1f3e0741e353d4c1e57389dd5ed04e0 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:40:49 +0530 Subject: [PATCH 04/11] BubbleSort | Added doctests --- Sorts/BubbleSort.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 296d4a49c4..e1ae967162 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -9,6 +9,24 @@ * it is greater than itself, if so we swap them. */ +/* +* Doctests +* +* > bubbleSort([5, 4, 1, 2, 3]) +* [1, 2, 3, 4, 5] +* > bubbleSort([]) +* [] +* > bubbleSort([1, 2, 3]) +* [1, 2, 3] +* +* > alternativeBubbleSort([5, 4, 1, 2, 3]) +* [1, 2, 3, 4, 5] +* > alternativeBubbleSort([]) +* [] +* > alternativeBubbleSort([1, 2, 3]) +* [1, 2, 3] +*/ + /* * Using 2 for loops */ @@ -34,14 +52,12 @@ function bubbleSort(items) { */ var array1 = [5, 6, 7, 8, 1, 2, 12, 14] // Before Sort -console.log('- Before Sort | Implementation using 2 for loops -') +console.log('\n- Before Sort | Implementation using 2 for loops -') console.log(array1) // After Sort console.log('- After Sort | Implementation using 2 for loops -') console.log(bubbleSort(array1)) - -/* Separating Console Logs */ -console.log("\n\n") +console.log('\n') /* * Using a while loop and a for loop @@ -67,8 +83,9 @@ function alternativeBubbleSort(arr) { */ var array2 = [5, 6, 7, 8, 1, 2, 12, 14] // Before Sort -console.log('- Before Sort | Implementation using a while loop and a for loop -') +console.log('\n- Before Sort | Implementation using a while loop and a for loop -') console.log(array2) // After Sort console.log('- After Sort | Implementation using a while loop and a for loop -') console.log(bubbleSort(array2)) +console.log('\n') \ No newline at end of file From 8ad54c8a45ca3510cf959c459e553331536c05c9 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:45:57 +0530 Subject: [PATCH 05/11] BubbleSort | Added Wikipedia Url --- Sorts/BubbleSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index e1ae967162..60f168116e 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -7,6 +7,8 @@ * the previous inner-loop iteration. By swapped I mean, in the * inner loop iteration, we check each number if the number proceeding * it is greater than itself, if so we swap them. +* +* Wikipedia: https://en.wikipedia.org/wiki/Bubble_sort */ /* From 5aba91a410a4b88c6a4e2e47b7f178ccc7a79902 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:55:26 +0530 Subject: [PATCH 06/11] BubbleSort | JS Standard fixes --- Sorts/BubbleSort.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 60f168116e..808bfc7457 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -2,7 +2,7 @@ * compares adjacent element and swaps thier position * The big O on bubble sort in worst and best case is O(N^2). * Not efficient. -* +* * In bubble sort, we keep iterating while something was swapped in * the previous inner-loop iteration. By swapped I mean, in the * inner loop iteration, we check each number if the number proceeding @@ -13,7 +13,7 @@ /* * Doctests -* +* * > bubbleSort([5, 4, 1, 2, 3]) * [1, 2, 3, 4, 5] * > bubbleSort([]) @@ -32,7 +32,7 @@ /* * Using 2 for loops */ -function bubbleSort(items) { +function bubbleSort (items) { const length = items.length for (let i = (length - 1); i > 0; i--) { @@ -64,7 +64,7 @@ console.log('\n') /* * Using a while loop and a for loop */ -function alternativeBubbleSort(arr) { +function alternativeBubbleSort (arr) { let swapped = true while (swapped) { @@ -90,4 +90,4 @@ console.log(array2) // After Sort console.log('- After Sort | Implementation using a while loop and a for loop -') console.log(bubbleSort(array2)) -console.log('\n') \ No newline at end of file +console.log('\n') From 5a5e4334d18ad07d0a7eccaaac8c7bf5f876e420 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:56:41 +0530 Subject: [PATCH 07/11] BubbleSort | using alternativeBubbleSort() in it's implementation --- Sorts/BubbleSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 808bfc7457..9f2fecac13 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -89,5 +89,5 @@ console.log('\n- Before Sort | Implementation using a while loop and a for loop console.log(array2) // After Sort console.log('- After Sort | Implementation using a while loop and a for loop -') -console.log(bubbleSort(array2)) +console.log(alternativeBubbleSort(array2)) console.log('\n') From 6f2a1cd617a2b2d0f2416f1f36412d941fdac4c9 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 12 Oct 2020 22:50:55 +0530 Subject: [PATCH 08/11] Cocktail Shaker Sort | Improved Comments and Formatting --- Sorts/CocktailShakerSort.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 337fa5f9f7..36df5e1bc3 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -1,15 +1,19 @@ -/* - * Cocktail shaker sort is a sort algorithm that is a bidirectional bubble sort - * more information: https://en.wikipedia.org/wiki/Cocktail_shaker_sort - * more information: https://en.wikipedia.org/wiki/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. + * + * 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) { for (let i = items.length - 1; i > 0; i--) { 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]] @@ -17,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]] @@ -30,11 +34,14 @@ function cocktailShakerSort (items) { } } -// Implementation of cocktailShakerSort - -var ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -cocktailShakerSort(ar) -// Array after sort -console.log(ar) +/** +* Implementation of Cocktail Shaker Sort +*/ +var array = [5, 6, 7, 8, 1, 2, 12, 14] +// Before Sort +console.log('\n- Before Sort | Implementation of Cocktail Shaker Sort -') +console.log(array) +// After Sort +console.log('- After Sort | Implementation of Cocktail Shaker Sort -') +console.log(cocktailShakerSort(array)) +console.log('\n') \ No newline at end of file From b2676dd792ef8c3a31718fab8a21132111f93cac Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 12 Oct 2020 22:51:30 +0530 Subject: [PATCH 09/11] Cocktail Shaker Sort | The function was not returning the items. Changed that. --- Sorts/CocktailShakerSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 36df5e1bc3..3256aaf8f7 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -32,6 +32,8 @@ function cocktailShakerSort (items) { return } } + + return items; } /** From 310746db5fe13dc4c7737c7a69c15d0866b0c783 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 12 Oct 2020 23:03:40 +0530 Subject: [PATCH 10/11] Cocktail Shaker Sort | Added Doctests --- Sorts/CocktailShakerSort.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 3256aaf8f7..e94973797d 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -1,13 +1,24 @@ /** * 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, + * 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. - * + * * Wikipedia (Cocktail Shaker Sort): https://en.wikipedia.org/wiki/Cocktail_shaker_sort * 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 swapped = false @@ -28,12 +39,9 @@ function cocktailShakerSort (items) { swapped = true } } - if (!swapped) { - return - } } - return items; + return items } /** @@ -46,4 +54,4 @@ console.log(array) // After Sort console.log('- After Sort | Implementation of Cocktail Shaker Sort -') console.log(cocktailShakerSort(array)) -console.log('\n') \ No newline at end of file +console.log('\n') From 34004845c1c733fbe13eb74f2a724459abf12555 Mon Sep 17 00:00:00 2001 From: marsonya Date: Mon, 12 Oct 2020 23:04:28 +0530 Subject: [PATCH 11/11] Cocktail Shaker Sort | Removed unused variable --- Sorts/CocktailShakerSort.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index e94973797d..d22ebedddb 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -21,14 +21,12 @@ function cocktailShakerSort (items) { for (let i = items.length - 1; i > 0; i--) { - let swapped = false let j // 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]] - swapped = true } } @@ -36,7 +34,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 } } }