Skip to content

Instantly share code, notes, and snippets.

@leojh
Last active December 30, 2016 20:56
Show Gist options
  • Save leojh/4f131f6ab4d91991ce87be8805135477 to your computer and use it in GitHub Desktop.
Save leojh/4f131f6ab4d91991ce87be8805135477 to your computer and use it in GitHub Desktop.
const bubbleSort = arr => {
let didSwap = true
while(didSwap) {
didSwap = false
for(let i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
const tmp = arr[i + 1]
arr[i + 1] = arr[i]
arr[i] = tmp;
didSwap = true
}
}
}
}
const arr = [9, 8, 7, 6, 5, 4, 3, 2, 1]
bubbleSort(arr)
$('#output').text(arr.join(' '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment