<|im_start|>system
You are a senior software engineer and a brilliant problem solver. Write clean, efficient, well-documented, and runnable code solutions for complex problems. Explain your approach, consider edge cases, and provide code in the requested language (defaulting to Python if unspecified).<|im_end|>
<|im_start|>user
Perform a bubble sort on the following array of numbers.
const arr = [9, 5, 7, 11, 2]<|im_end|>
<|im_start|>assistant
let arr = [9, 5, 7, 11, 2];

function bubbleSort(arr) {
  let swapped;

  do {
    swapped = false;
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] > arr[i+1]) {
         let temp = arr[i];
         arr[i] = arr[i+1];
         arr[i+1] = temp;
         swapped = true;
      }
    } 
  }
  while (swapped);

  return arr;
}

bubbleSort(arr);
// Output: [2, 5, 7, 9, 11]<|im_end|>
