From 842196398e56c868571c4b20cadd68941ebf1e4f Mon Sep 17 00:00:00 2001 From: lucaspalomodevelop Date: Thu, 3 Mar 2022 19:16:40 +0100 Subject: [PATCH] add asc/desc --- bubblesort.js | 12 ++++++++++-- index.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/bubblesort.js b/bubblesort.js index 93236a6..b511466 100644 --- a/bubblesort.js +++ b/bubblesort.js @@ -1,10 +1,10 @@ -function bubblesort(array) { +function bubblesort(array, asc = true) { let swap; let sorted; do { sorted = true; for (let i = 0; i < array.length; i++) { - if (array[i] > array[i + 1]) { + if (getAscExpression(array[i], array[i + 1], asc)) { swap = array[i]; array[i] = array[i + 1]; array[i + 1] = swap; @@ -15,4 +15,12 @@ function bubblesort(array) { return array; } +function getAscExpression(first, secound, asc) { + if (asc) { + return first > secound; + } else { + return secound > first; + } +} + module.exports = bubblesort; diff --git a/index.js b/index.js index 515d5ac..94ea6f9 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,3 @@ let bubblesort = require("./bubblesort"); -console.log(bubblesort([1, 3, 2])); +console.log(bubblesort([1, 3, 2], true));