Created
June 26, 2018 11:53
-
-
Save elitenomad/9c6fc5e486e5b770a439a98a2dd30db9 to your computer and use it in GitHub Desktop.
Bubble sort Javascript implementation: O(n^2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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