Created
February 28, 2019 03:10
-
-
Save joelbarbosa/473d75d316a762b19eb3c0c6868d0856 to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bublesort(arr1) { | |
const arr = [...arr1]; | |
let swap | |
do { | |
swap = false; | |
for (i=0; i < arr.length-1; i++) { | |
if (arr[i] > arr[i+1]) { | |
let tmp = arr[i]; | |
arr[i] = arr[i+1]; | |
arr[i+1] = tmp; | |
swap = true | |
} | |
} | |
}while(swap); | |
return arr; | |
} | |
const a1 = [1, 2, 4, 0, 10, 3, 6, 5]; | |
console.log(bublesort(a1)) | |
// alternative sort | |
function _sort(a, b) { | |
return a - b; | |
} | |
const sort = a1.sort(_sort) | |
console.log(sort) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment