Skip to content

Instantly share code, notes, and snippets.

@mustafadalga
Last active October 9, 2021 18:09
Show Gist options
  • Save mustafadalga/558cc4ffbdc2cb655f0cb1bff4d0754d to your computer and use it in GitHub Desktop.
Save mustafadalga/558cc4ffbdc2cb655f0cb1bff4d0754d to your computer and use it in GitHub Desktop.
Bubble sort algorithm
function bubbleSort(array) {
let noSwaps;
for (let i = array.length; i > 0; i--) {
noSwaps = true;
for (let j = 0; j < i - 1; j++) {
if (array[j] > array[j + 1]) {
let temp = array[j];
array[j] = array[j + 1]
array[j + 1] = temp;
noSwaps = false;
}
}
if (noSwaps) break;
}
return array;
}
const numberList=Array.from({length: 100}, () => Math.floor(Math.random() * 100));
console.log(bubbleSort(numberList))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment