mirror of
https://github.com/Yet-Another-DreamTeam/js_algorithms.git
synced 2026-03-13 00:07:24 +00:00
add asc/desc
This commit is contained in:
parent
6266a05dd1
commit
842196398e
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user