Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active November 24, 2019 00:40
Show Gist options
  • Save okovalov/22b9324fd9905ccdcf5a0b31568a35aa to your computer and use it in GitHub Desktop.
Save okovalov/22b9324fd9905ccdcf5a0b31568a35aa to your computer and use it in GitHub Desktop.
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