Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created November 2, 2014 15:30
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/98b3f9b890a00dd0637e to your computer and use it in GitHub Desktop.
Save shigemk2/98b3f9b890a00dd0637e to your computer and use it in GitHub Desktop.
function bswap(n, x) {
if(n <= 0) 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 (n == 0) return x;
bswap(n - 1, x);
var x1 = x.shift();
return [x1].concat(bubble(x));
};
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