A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
function bubbleSort(array: number[]) {const n = array.length;let newArray = [...array];for (let i = 0; i < n - 1; i++) {for (let j = 0; j < n - i - 1; j++) {if (newArray[j] > newArray[j + 1]) {const temp = newArray[j];newArray[j] = newArray[j + 1];newArray[j + 1] = temp;}}}return newArray;}
Best Case: O(n) - When the array is already sorted
Average Case: O(n²)
Worst Case: O(n²) - When the array is sorted in reverse order