Skip to content

Instantly share code, notes, and snippets.

@SparrowMike
Created November 6, 2022 02:46
Show Gist options
  • Save SparrowMike/cdf3949d8c2d673074042b8191e0e1bd to your computer and use it in GitHub Desktop.
Save SparrowMike/cdf3949d8c2d673074042b8191e0e1bd to your computer and use it in GitHub Desktop.
function swap(arr, idx1, idx2) {
let temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
// [arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]]
}
function bubbleSort(arr) {
let noSwap
for (let i = arr.length; i > 0; i--) {
noSwap = true;
for (let j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1)
noSwap = false
}
}
if (noSwap) break
}
return arr
}
bubbleSort([41,6,23,9,4,6,12,32,23,13,2,1,3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment