From 5742bf50097731f06cdfbc8a9232000f9b5f0bc2 Mon Sep 17 00:00:00 2001 From: Abose Ukhun Date: Mon, 16 Oct 2017 11:23:46 +0100 Subject: [PATCH 1/5] add to bubble sort information about big O complexity and efficiency --- Sorts/bubblesort.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sorts/bubblesort.js b/Sorts/bubblesort.js index 9f9659c7e..4bcd3904f 100644 --- a/Sorts/bubblesort.js +++ b/Sorts/bubblesort.js @@ -1,5 +1,7 @@ /*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. */ function bubbleSort(items) { var length = items.length; @@ -25,3 +27,5 @@ console.log(ar); bubbleSort(ar); //Array after sort console.log(ar); + + From 6de559d0e991c224f59aefab567f33d062b7f3fc Mon Sep 17 00:00:00 2001 From: Abose Ukhun Date: Mon, 16 Oct 2017 12:03:41 +0100 Subject: [PATCH 2/5] add alternative implementation of bubble sort using a while loop --- Sorts/bubblesort.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Sorts/bubblesort.js b/Sorts/bubblesort.js index 4bcd3904f..a22832d26 100644 --- a/Sorts/bubblesort.js +++ b/Sorts/bubblesort.js @@ -28,4 +28,37 @@ bubbleSort(ar); //Array after sort 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. +*/ + +function bubbleSort(arr){ + var swapped = true; + while(swapped){ + var swapped = false; + for(var i = 0; i < arr.length; i++){ + if(arr[i] > arr[i + 1]){ + var temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + swapped = true; + } + } + } + return arr; +} + +//test +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(bubbleSort(array)); + From 0551dbab3784a27d783e7e09340cd94eabea0c1e Mon Sep 17 00:00:00 2001 From: Abose Ukhun Date: Mon, 16 Oct 2017 12:47:45 +0100 Subject: [PATCH 3/5] remove changes made on adding alternative implementation of bubble sort --- Sorts/bubblesort.js | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/Sorts/bubblesort.js b/Sorts/bubblesort.js index a22832d26..4bcd3904f 100644 --- a/Sorts/bubblesort.js +++ b/Sorts/bubblesort.js @@ -28,37 +28,4 @@ bubbleSort(ar); //Array after sort 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. -*/ - -function bubbleSort(arr){ - var swapped = true; - while(swapped){ - var swapped = false; - for(var i = 0; i < arr.length; i++){ - if(arr[i] > arr[i + 1]){ - var temp = arr[i]; - arr[i] = arr[i + 1]; - arr[i + 1] = temp; - swapped = true; - } - } - } - return arr; -} - -//test -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(bubbleSort(array)); - From 9dbf000df728941e147133aef97ed446507abdc7 Mon Sep 17 00:00:00 2001 From: Abose Ukhun Date: Mon, 16 Oct 2017 12:56:02 +0100 Subject: [PATCH 4/5] add additional implementation using a while loop for educational purposes --- Sorts/bubblesort.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Sorts/bubblesort.js b/Sorts/bubblesort.js index 4bcd3904f..307b2b2b0 100644 --- a/Sorts/bubblesort.js +++ b/Sorts/bubblesort.js @@ -23,9 +23,43 @@ function bubbleSort(items) { 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(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. +*/ + +function bubbleSort(arr){ + var swapped = true; + while(swapped){ + var swapped = false; + for(var i = 0; i < arr.length; i++){ + if(arr[i] > arr[i + 1]){ + var temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + swapped = true; + } + } + } + return arr; +} + +//test +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(bubbleSort(array)); + From 28343d2cc3c42cc5e0ae24f21bb7b8ab4e9e330e Mon Sep 17 00:00:00 2001 From: Abose Ukhun Date: Mon, 16 Oct 2017 13:05:33 +0100 Subject: [PATCH 5/5] add console printing for after sorting --- Sorts/bubblesort.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sorts/bubblesort.js b/Sorts/bubblesort.js index 307b2b2b0..d398a5ad6 100644 --- a/Sorts/bubblesort.js +++ b/Sorts/bubblesort.js @@ -27,6 +27,7 @@ console.log("-----before sorting-----") console.log(ar); bubbleSort(ar); //Array after sort +console.log("-----after sorting-----") console.log(ar); /*alternative implementation of bubble sort algorithm.