Last active
November 28, 2016 14:37
-
-
Save kdnk/874a905426ddea2ffecaf485413e311a to your computer and use it in GitHub Desktop.
This file contains 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
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