Skip to content

Instantly share code, notes, and snippets.

@kdnk
Last active November 28, 2016 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdnk/874a905426ddea2ffecaf485413e311a to your computer and use it in GitHub Desktop.
Save kdnk/874a905426ddea2ffecaf485413e311a to your computer and use it in GitHub Desktop.
main()
function main () {
let arr = [5, 8, 4, 2, 6, 1, 3, 9]
printArray(arr)
bubleSort(arr, arr.length)
printArray(arr)
}
function swap (a, idx1, idx2) {
const t = a[idx1]
a[idx1] = a[idx2]
a[idx2] = t
}
function bubleSort (a, n) {
for (let i = 0; i < n - 1; i++) {
for (let j = n - 1; j > i; j--) {
if (a[j] < a[j - 1]) {
swap(a, j, j - 1)
}
}
}
}
function printArray (a) {
a.join(' ')
console.log(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment