Skip to content

Instantly share code, notes, and snippets.

@assertchris
Created April 20, 2015 00:53
Show Gist options
  • Save assertchris/4b4091ed5ed99bb23e20 to your computer and use it in GitHub Desktop.
Save assertchris/4b4091ed5ed99bb23e20 to your computer and use it in GitHub Desktop.
function bubble(data) {
var length = data.length;
while (length) {
for (var i = 1; i < length; i++) {
var prev = data[i - 1];
var next = data[i];
if (prev > next) {
data[i] = prev;
data[i - 1] = next;
}
}
length--;
}
return data;
}
console.log(
bubble([5, 4, 7, 3, 2, 8, 1, 9, 0, 6])
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment