Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

bubble sort js

function bubbleSort(nums) {
  let swapped = false;
  let iterations = 0;
  do {
    swapped = false;
    for (let i = 0; i < nums.length - iterations - 1; i++) {
      if (nums[i] > nums[i + 1]) {
        [nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
        swapped = true;
      }
    }
    iterations++;
  } while (swapped);
  return nums;
}
Source by learn.coderslang.com #
 
PREVIOUS NEXT
Tagged: #bubble #sort #js
ADD COMMENT
Topic
Name
2+2 =