Skip to content

Instantly share code, notes, and snippets.

@jlcarrascof
Created September 28, 2023 14:16
Show Gist options
  • Save jlcarrascof/6b84c7f9bb82b7cf925ec781e1d139a3 to your computer and use it in GitHub Desktop.
Save jlcarrascof/6b84c7f9bb82b7cf925ec781e1d139a3 to your computer and use it in GitHub Desktop.
Bubble sort in Pair Programming
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