Bubble Sort

Implement bubbleSort

Given an array of integers, sort it in non-decreasing order using **Bubble Sort** — repeatedly sweep through the array, swapping adjacent elements that are out of order, until the whole array is sorted. Each full sweep pushes ("bubbles") the largest remaining value to its correct spot at the end, one position closer with every pass. It's one of the simplest sorting algorithms to reason about, and a natural first place to learn a key optimization idea: noticing when no work was done in a pass, and stopping early instead of grinding through passes that can't possibly change anything.

Example 1:

Input: arr = [7,2,9,4,2,8]

Output: [2,2,4,7,8,9]

Example 2:

Input: arr = []

Output: []

Example 3:

Input: arr = [5]

Output: [5]

+ 5 hidden test cases run on Submit.

Constraints:

  • 0 ≤ arr.length ≤ 200
  • -1000 ≤ arr[i] ≤ 1000
  • Sort in non-decreasing order

arr =

[7, 2, 9, 4, 2, 8]