function bubbleSort(nums) {
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
[nums[j], nums[j + 1]] = [nums[j + 1], nums[j]];
}
}
}
return nums;
}
Explanation: Bubble sort repeatedly swaps adjacent elements if they are in the wrong order.
β± O(nΒ²) | πΎ O(1)