Skip to content

Instantly share code, notes, and snippets.

@alexeykomov
Created August 26, 2016 08:40
Show Gist options
  • Save alexeykomov/fdda60945070d22e518a04e403751f6d to your computer and use it in GitHub Desktop.
Save alexeykomov/fdda60945070d22e518a04e403751f6d to your computer and use it in GitHub Desktop.
function bubbleSort(aInput) {
const output = [...aInput];
for (let padding = 0; padding < output.length; padding++) {
for (let counter = 1; counter < output.length - padding; counter++){
const prev = output[counter - 1];
const next = output[counter];
if (prev > next) {
swap(output, counter - 1, counter);
}
}
}
return output;
}
function swap(aArray, aPrevIndex, aNextIndex) {
const temp = aArray[aPrevIndex];
aArray[aPrevIndex] = aArray[aNextIndex];
aArray[aNextIndex] = temp;
}
console.log(bubbleSort([8, 1, 3, 4, 5]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment