Created
September 28, 2023 14:16
-
-
Save jlcarrascof/6b84c7f9bb82b7cf925ec781e1d139a3 to your computer and use it in GitHub Desktop.
Bubble sort in Pair Programming
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
function bubbleSort(arr) { | |
const n = arr.length; | |
let swapped; | |
do { | |
swapped = false; | |
for (let i = 0; i < n - 1; i++) { | |
if (arr[i] > arr[i + 1]) { | |
// Swap the elements if they are in the correct order | |
const temp = arr[i]; | |
arr[i] = arr[i + 1]; | |
arr[i + 1] = temp; | |
swapped = true; | |
} | |
} | |
} while (swapped); | |
return arr; | |
} | |
// Example: | |
const arr = [64, 34, 25, 12, 22, 11, 90]; | |
console.log("Original array:", arr); | |
bubbleSort(arr); | |
console.log("Ordered array:", arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment