Answers for "advantages of bubble sort in js"

3

javascript bubble sort

const bubbleSort = array => {
  const arr = Array.from(array); // avoid side effects
  for (let i = 1; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i; j++) { // starts up to length - 1
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
};

console.log(bubbleSort([4, 9, 2, 1, 5]));
Posted by: Guest on March-29-2021
1

how to do bubble sort in javascript

function BubbleSort(arr) {
      const sortedArray = Array.from(arr);
      let swap;
      do {
        swap = false;
        for (let i = 1; i < sortedArray.length; ++i) {
          if (sortedArray[i - 1] > sortedArray[i]) {
            [sortedArray[i], sortedArray[i - 1]] = [sortedArray[i - 1], sortedArray[i]];
            swap = true;
          }
        }
      } while (swap)
      return sortedArray;
    }

    console.log(BubbleSort([7,99,1,88,34,2,90,7]));
Posted by: Guest on June-05-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language