Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

// 2. bubbleSort/*from  www  .  java 2 s . c  o  m*/
// Your version can modify the original array.

Array.prototype.bubbleSort = function (){
  let isSorted = false; // sorted variable equal to false; this is just to define the boolean variable

  while(!isSorted){ // until sorted variable is true, this code will run
    isSorted = true;

    for (var i = 0; i < this.length; i++){
      if (this[i] > this[i+1]){ // if it never enters this if statement, isSorted will stay true
        let earlierNum = this[i];
        this[i] = this[i + 1];
        this[i + 1] = earlierNum;

        isSorted = false; // sorted variable is now set to FALSE, so loop will run again.
      }
    }
  }
  return this;
}

// console.log([2,5,6,3,8].bubbleSort());

Related

  1. bubbleSort()
    function factors(num) {
      for(let i=0; i <=num; i++ ) {
        if (num % i === 0) {
          console.log(i);
    Array.prototype.bubbleSort = function () {
      let sorted = false;
    ...
    
  2. bubbleSort()
    var fibonacci = function(num) {
      var fibs = [0, 1];
      if (num < 3) {
        return fibs.slice(0, num);
      while (fibs.length < num) {
        fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]);
      return fibs;
    ...
    
  3. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var temp;
      for(var i = 0; i < this.length - 1; i++) {
        for(var z = 0; z <= this.length - i - 1; z++) {
          if(this[z] > this[z + 1]) {
            temp = this[z + 1];
            this[z + 1] = this[z];
            this[z] = temp;
      return this;
    var a = [23,34,55,24,51,56,27,29];
    a.bubbleSort();
    console.log(a);
    
  4. bubbleSort()
    function bubbleSort (arr) {
      var swapped = true;
      var swapCount = 0;
      while (swapped === true) {
        swapped = false;
        for (var i = 0; i < arr.length-1; i++) {
          var curr = arr[i];
          var next = arr[i+1];
          if (curr > next) {
    ...
    
  5. bubbleSort()
    function bubbleSort(arr){
      var bubbledArr = arr;
      var isSwapped = false;
      var count = 0;
      while(!isSwapped) {
        isSwapped = false;
        var moves = 0;
        for(var i = 0; i < bubbledArr.length -1; i++){
          var curr = bubbledArr[i];
    ...
    
  6. bubbleSort()
    Array.prototype.bubbleSort = function(){
      var len = this.length;
      var exchange = false;
      var temp;
      for(var i = 1;i < len;i++){
        for(var j = 0;j < len - i;j++){
          if(this[j] > this[j+1]){
            temp = this[j];
            this[j] = this[j+1];
    ...
    
  7. bubbleSort()
    Array.prototype.bubbleSort = function () {
        for (var i = this.length-1; i > 0; i--) {
            for (var j = 0; j < i; j++) {
                if(this[j] > this[j+1]) {
                    this.swap(j, j+1);
    };
    ...
    
  8. bubbleSort(arr)
    Array.prototype.bubbleSort = function(arr) {
      if(arr !== undefined) {
        this.array = arr.map(function(element){
          return element;
        });
      } else {
        this.array = this.map(function(element){
          return element;
        });
    ...
    
  9. bubbleSort_bubbleSorter;
    function _bubbleSorter(sortMe){
      if(!Array.isArray(sortMe)){
        throw new TypeError('Your sortMe needs to be an array');
      for(var j = 0; j < sortMe.length; j++){
        for(var i = 0; i < sortMe.length; i++){
          if(sortMe[i] > sortMe[i+1]){
            var tempNum = sortMe[i];
            sortMe[i] = sortMe[i+1];
    ...