Skip to content

Instantly share code, notes, and snippets.

@elitenomad
Created June 26, 2018 11:53
Show Gist options
  • Save elitenomad/9c6fc5e486e5b770a439a98a2dd30db9 to your computer and use it in GitHub Desktop.
Save elitenomad/9c6fc5e486e5b770a439a98a2dd30db9 to your computer and use it in GitHub Desktop.
Bubble sort Javascript implementation: O(n^2)
const bubble_sort = (array) => {
for(let i = 0 ; i < array.length - 1 ; i++) {
for(let j = 0; j < array.length - i - 1; j++){
if(array[j] > array[j + 1]){
const temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
a = [32,23,34,55,23,73,1,35,202]
result = bubble_sort(a);
console.log(`Sorted array is ${result}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment