Create your own
teaching resources

Sign up for a free account to try! It takes less than 20 seconds

Teacherbot 27 Jan, 08:30:54

//Bubble sort let numbers = [5, 8, 3, 6, 4];

function bubbleSort(nums) { for (let i = 0; i < nums.length; i++) { for (let j = 0; j < nums.length - i; j++) { if (nums[j] > nums[j + 1]) { [nums[j], nums[j+1]] = [nums[j+1], nums[j]] //swap numbers } } }

return nums; }

console.log(bubbleSort(numbers)); // prints [3, 4, 5, 6, 8]