Answers for "javascript implement Bubble Sort"

1

bubble sort javascript

function bubbleSort(array) {
  const len = array.length;
  const retArray = array;
  for (let i = 0; i < len; i++) {
    for (let j = 0; j < len - i; j++) {
      const a = array[j];
      if (a !== array[-1]) {
        const b = array[j + 1];
        if (a > b) {
          retArray[j] = b;
          retArray[j + 1] = a;
        }
      }
    }
  }
  return retArray;
}
bubbleSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
Posted by: Guest on November-11-2020

Code answers related to "javascript implement Bubble Sort"

Code answers related to "Javascript"

Browse Popular Code Answers by Language