Skip to content

Instantly share code, notes, and snippets.

@eengineergz
Created February 27, 2021 05:04
Show Gist options
  • Save eengineergz/fd4acc0c89033bd219ebf9d3ec40b053 to your computer and use it in GitHub Desktop.
Save eengineergz/fd4acc0c89033bd219ebf9d3ec40b053 to your computer and use it in GitHub Desktop.
// Bubble Sort
function bubble(array) {
let sorted = true;
for (let i = 0; i < array.length; i++) {
let num1 = array[i];
let num2 = array[i + 1];
if (num1 > num2) {
array[i + 1] = num1;
array[i] = num2;
sorted = false;
}
}
if (sorted) {
return array;
} else {
return bubble(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment