Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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;
};
Comment

javascript bubble sort

const bubbleSort = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i; j++) {
      if (arr[j] > arr[j + 1]) {
        let tmp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = tmp;
      }
    }
  }
  return arr;
}
Comment

js bubble sort

// Bubble sort is a sorting algorithm that sorts data in an array by looping through the array
// and checking if the current item is greater than the next item.
// It will continue to loop through, swapping the items until they are sorted.
// If no swaps are made during a pass, then the data is sorted and we break the loop.

const bubbleSort = (arr) => {
  // This variable is used to either continue or stop the loop
  let continueSorting = true;

  // while continueSorting is true
  while (continueSorting) {
    // Here we are setting continueSorting to false. Because below we check to see if a swap is made,
    // if a swap is made, then we continue sorting. If no swaps were made, then were are done sorting,
    // and stop our while loop.
    continueSorting = false

    // loop through the arr from 0 to next to last
    for(let i = 0; i < arr.length - 1; i++) {
      // check if we need to swap
      if(arr[i] > arr[i + 1]) {
        // swap
        let temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;

        // since a swap was made, we want to continue sorting
        continueSorting = true;
      }
    }
  }

  // After all swaps have been made, then we return our sorted array
  return arr;
}

// Tests
console.log(bubbleSort([9, 8, 7, 6, 5, 4, 3, 2, 1] ));
console.log(bubbleSort([1, 2, 3, 4, 5, 6, 7, 8, 9] ));
Comment

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;
}
Comment

bubble sort javascript

function bubbleSort(arr){ 
   const length = arr.length;
   let isSwapped = false;
  
    for(let i = 0; i < length; i++){
        for(let j = 0; j < length; j++){
            if(arr[j] > arr[j+1]){
                //swap
                let temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
              isSwapped = true;
            }
        }
      // IF no two elements were swapped by inner loop, then break
      //i.e. array is already sorted
      if(!isSwapped) break;
    }
    return arr;
}

let a = [99,44,6,2,1,5,63,87,283,4,0];
console.log(bubbleSort(a));
Comment

Bubble sort code implementation in javascript

function bubbleSort(array) {
    let isSwapped;

    for(let i = array.length; i > 0; i--) {
        isSwapped = false;

        for(let j = 0; j < i - 1; j++) {
            if(array[j] > array[j + 1]) {
                [array[j], array[j+1]] = [array[j+1], array[j]];
                isSwapped = true;
            }
        }

        if(!isSwapped) {
            break;
        }
    }
    return array;
}
Comment

Bubble Sort Javascript

//Bubble Sort using Javascript

let bubbleSort = (inputArr) => {
    let len = inputArr.length;
    for (let i = 0; i < len; i++) {
        for (let j = 0; j < len; j++) {
            if (inputArr[j] > inputArr[j + 1]) {
                let tmp = inputArr[j];
                inputArr[j] = inputArr[j + 1];
                inputArr[j + 1] = tmp;
            }
        }
    }
    return inputArr;
};
Comment

Bubble Sort Javascript

/*A slightly better solution involves tracking a variable called "checked" 
which is initially set to FALSE and becomes true when there is a swap during the iteration. 
Running this code on a do while loop to run the sorting function only when "checked" is true 
ensures that the function will not run on a sorted array more than once.*/
[visualization](https://visualgo.net/en/sorting?slide=1);

let bubbleSort = (inputArr) => {
    let len = inputArr.length;
    let checked;
    do {
        checked = false;
        for (let i = 0; i < len; i++) {
            if (inputArr[i] > inputArr[i + 1]) {
                let tmp = inputArr[i];
                inputArr[i] = inputArr[i + 1];
                inputArr[i + 1] = tmp;
                checked = true;
            }
        }
    } while (checked);
    return inputArr;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular call child method from parent 
Javascript :: js check if this last index in foreach 
Javascript :: if text exists in element using javascript 
Javascript :: how to delete a letter from a string in javascript 
Javascript :: how to concatenate strings and variables in javascript 
Javascript :: js get file location 
Javascript :: angular list contains property 
Javascript :: javascript iterate over map keys 
Javascript :: how to empty a filled input in cypress 
Javascript :: wait js 
Javascript :: shorthand if statment in js 
Javascript :: react render after data loaded 
Javascript :: javascript html append 
Javascript :: focus element javascript 
Javascript :: counter app in react class based component 
Javascript :: Converting string to json object 
Javascript :: reverse string with recursion 
Javascript :: babylon js camera position 
Javascript :: linking html with javascript 
Javascript :: js create element 
Javascript :: get id of an element 
Javascript :: moment timezone set clock in another timezone 
Javascript :: set localstorage 
Javascript :: require statement not part of import statement 
Javascript :: using fetch api 
Javascript :: spotify web player 
Javascript :: get position of element relative to document 
Javascript :: regex to check 8 < length < 16, one uppercase, one lowercase and must have at least one number and one special character 
Javascript :: node get package.json version 
Javascript :: invoke in js 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =