Master essential programming concepts, data structures, and CSS mechanics with interactive, live-executing simulations.
An interactive simulation demonstrating how bubble sort iteratively swaps adjacent elements to sort an array. Ideal for learning computational complexity.
function bubbleSort(arr: number[]): number[] {
const n = arr.length;
let swapped: boolean;
for (let i = 0; i < n - 1; i++) {
swapped = false;
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
const temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) break;
}
return arr;
}