function bubbleSort(arr) {
const a = [...arr];
for (let i = 0; i < a.length; i++)
for (let j = 0; j < a.length - i - 1; j++)
if (a[j] > a[j+1]) [a[j], a[j+1]] = [a[j+1], a[j]];
return a;
}
bubbleSort([5, 3, 1]); // [1, 3, 5]Bubble sort repeatedly swaps adjacent elements that are out of order β O(n^2).
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they're in the wrong order. The pass through the list is repeated until the list is sorted.
Time Complexity: O(nΒ²) Space Complexity: O(1)
function bubbleSort(arr: number[]): number[] {
// Implement bubble sort
// Return the sorted array
}
bubbleSort([64, 34, 25, 12, 22]) // [12, 22, 25, 34, 64]