Skip to content

Instantly share code, notes, and snippets.

@iamamit-107
Created May 9, 2020 16:32
Show Gist options
  • Save iamamit-107/aca8398e6eebafc65f373acf3f643a95 to your computer and use it in GitHub Desktop.
Save iamamit-107/aca8398e6eebafc65f373acf3f643a95 to your computer and use it in GitHub Desktop.
function bubbleSort(arr) {
//sort the array
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
const lesser = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = lesser;
}
}
}
//returning the array
return arr;
}
console.log(bubbleSort([10, 4, 3, 8]));
//output : [ 3, 4, 8, 10 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment