Implement bubbleSort(array), which returns a new array sorted in ascending numeric order using the classic bubble sort algorithm. You repeatedly walk the array comparing each adjacent pair and swapping them when they are out of order; after each full pass the largest remaining value has 'bubbled' up to its final spot at the end. Include the standard optimization: stop early once a pass completes without making any swaps.
// array: number[] — the numbers to sort. NOT mutated.
// returns: number[] — a NEW array, sorted ascending (numeric order).
function bubbleSort(array): number[];
bubbleSort([3, 1, 2]);
// → [1, 2, 3]
bubbleSort([5, 4, 3, 2, 1]);
// → [1, 2, 3, 4, 5]
[10, 2] sorts to [2, 10], not [10, 2]. (The built-in [].sort() would get this wrong without a comparator.)swapped flag per pass; if a pass makes zero swaps the array is already sorted, so break out instead of doing more useless work.[]; a single element returns itself; duplicates and negative numbers all sort normally.array[j] and array[j + 1].