Given an array of integers nums, sort the array in ascending order using the Bubble Sort algorithm — repeatedly step through the array, compare each pair of adjacent elements, and swap them if they're in the wrong order. Each full pass "bubbles" the largest remaining element to its correct position at the end.
nums = [5,2,9,1,5,6][1,2,5,5,6,9]Invariant: after pass i completes, the last i elements are the i largest values, in sorted position. Each inner pass "bubbles" the current largest unsorted element rightward one swap at a time until it reaches its resting place. The early-exit swapped flag turns best-case (already sorted) input into O(n).