Last active
November 24, 2019 00:40
-
-
Save okovalov/22b9324fd9905ccdcf5a0b31568a35aa to your computer and use it in GitHub Desktop.
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
const bubbleSort = arr => { | |
const { length } = arr | |
for (let i = 0 ; i < length; i++) { | |
for (let j = 0; j < length - i - 1; j++) { | |
if (arr[j] > arr[j+1]) { | |
const temp = arr[j] | |
arr[j] = arr[j + 1] | |
arr[j+1] = temp | |
} | |
} | |
} | |
return arr | |
} | |
const arr = [4,1,6,9,3,2,8,7,5] | |
const result = bubbleSort(arr) | |
console.time('test') | |
console.log(result) // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] test: 5.654ms | |
console.timeEnd('test') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment