Answers for "does javascript sort use bubble sort?"

17

bubble sort javascript

bubbleSort(Array) {
    let len = Array.length;
    for (let i = 0; i < len; i++) { //you can also use "for in", so you don't need the variable "len"
        for (let j = 0; j < len; j++) {
            if (Array[j] > Array[j + 1]) {
                let tmp = Array[j];
                Array[j] = Array[j + 1];
                Array[j + 1] = tmp;
            }
        }
    }
    return Array;
};
Posted by: Guest on March-18-2020
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"

Browse Popular Code Answers by Language