Last active
January 25, 2022 02:09
-
-
Save kevinjie/e3e70bffafa1cf0bbb4aef48f50664a4 to your computer and use it in GitHub Desktop.
bubbleSort
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
export default function sort(originalArray) { | |
const arr = [...originalArray] | |
let N = arr.length | |
let swapped = false | |
while (N > 0) { | |
swapped = false | |
for(let j = 0; j < N - 1; j += 1) { | |
if (arr[j] > arr[j+1]) { | |
[arr[j+1], arr[j]] = [arr[j], arr[j+1]] | |
swapped = true | |
} | |
} | |
N-- | |
if (!swapped) { | |
return arr | |
} | |
} | |
return arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment