From 95929c4552a3791cbe4c28f73186efb1296ff869 Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 9 Oct 2020 13:27:14 +0530 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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 */ /*