Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created November 2, 2014 15:33
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 shigemk2/81eca00245d471dd4a3f to your computer and use it in GitHub Desktop.
Save shigemk2/81eca00245d471dd4a3f to your computer and use it in GitHub Desktop.
function bswap(n, x) {
if(n <= 1) return x;
if(x[n] < x[n-1]) {
var bs = x[n];
x[n] = x[n-1];
x[n-1] = bs;
}
return bswap(n - 1, x);
};
function bubble(x) {
var n = x.length;
if (x.length == 0) return x;
return bubble(bswap(n - 1, x.slice(1, x.length)));
};
console.log(bubble([4, 6, 9, 8, 3, 5, 1, 7, 2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment