Javascript Array bubbleSort() algorithm

Description

Javascript Array bubbleSort() algorithm


a = [21,3,23,7,3,2]/*from  ww w . jav a  2s  . co m*/

Array.prototype.bubbleSort = function () {
  var sorted = false;

  do {
    sorted = true;

    for( var i = 0; i < this.length - 1; i++){
      if(this[i] > this[i+1]) {
        sorted = false;
        temp = this[i];
        this[i] = this[i+1];
        this[i+1] = temp;
      };
    };
  } while(!sorted);

  return this;
};

console.log(a.bubbleSort());

Javascript Array bubblesort()


Array.prototype.bubblesort = function() {
    var done = false;
    while (!done) {
        done = true;//  www .ja v a2s.co m
        for (var i = 1; i<this.length; i++) {
            if (this[i-1] > this[i]) {
                done = false;
                [this[i-1], this[i]] = [this[i], this[i-1]]
            }
        }
    }
    return this;
}

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function() {
  var len = this.length
  var tmp//from  w w w .  java  2  s . c om

  for(var i = 1; i < len; i++) {
    for(var j = i; j > 0; j--) {
      if(this[j] < this[j-1]) {
        tmp = this[j]
        this[j] = this[j-1]
        this[j-1] = tmp
      } else {
        break
      }
    }
  }
  return this
}

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function() {
  var sorted = false
  while (sorted === false){
    sorted = true;/*from  w  w w . jav  a2 s .co m*/
    for (var i=0; i < this.length - 1; i ++){
      if (this[i] > this[i+1]){
        this[i+1] = [this[i], this[i]=this[i+1]][0]
        sorted = false
      }
    }
  }
};

var array = [3,6,4,7,9,2,8]
array.bubbleSort();
console.log(array);

Javascript Array bubbleSort()



Array.prototype.bubbleSort = function () {
  let sorted = false;

  while (sorted === false) {
    sorted = true;// ww  w .j  a v a  2  s  .com
    for (var i = 0; i < this.length - 1; i++) {
      if (this[i] > this[i + 1]) {
        [this[i], this[i + 1]] = [this[i + 1], this[i]];
        sorted = false;
      }
    }
  }

  return this;
};

console.log([3, 1, 4, 2, 6, 5].bubbleSort());

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function () {
  var len = this.length;
  var i, j, tmp;//from   ww  w  . j  a  v a2 s  .  c  o m
  for (i = 0; i < len; i++) {
    for (j = len - 1; j > i; j--) {
      if (this[j] > this[j - 1]) {
        tmp = this[j - 1];
        this[j - 1] = this[j];
        this[j] = tmp;
      }
    }
  }
  return this;
}

var arr = [14, 6, 9, 4, 11];
var nArr = arr.bubbleSort();
console.log(nArr);

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function() {
  var notSorted = true;
  while (notSorted) {
    notSorted = false;/*  w w  w.  ja v  a 2s  .  c  om*/
    for (var i = 1; i < this.length; i++) {
      if (this[i-1] > this[i]) {
        var first = this[i-1];
        var second = this[i];
        this[i-1] = second;
        this[i] = first;
        notSorted = true;
      }
    }
  }
  return this;
}

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function(){
  var array = this
  var size = array.length
  var tmp/*from   www.  j av  a2 s  . co  m*/
  for(var i = 0; i < size; i++)
    for(var j = 0; j < size; j++)
      if(array[i] > array[j]){
        tmp = array[j]
        array[j] = array[i]
        array[i] = tmp
      }
}

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function(){
    for (var i = 0; i < this.length; i++){
        for (var j = i+1; j < this.length; j++){
            if (this[i] > this[j]){
                var t = this[i];
                this[i] = this[j];/*from w  w  w .  ja  va  2  s .  co  m*/
                this[j] = t;
            }
        }
    }
};

var a = [6,4,0, 3,-2,1];

console.log(a);

a.bubbleSort();

console.log(a);

Javascript Array BubbleSort()


Array.prototype.BubbleSort = function(){
    for(var i = 0; i < this.length - 1; i++){
        for(var j = i + 1; j < this.length; j++){
            if (this[i] > this[j]){
                var temp = this[i];
                this[i] = this[j];/*from   www.j  ava 2s  .  c o  m*/
                this[j] = temp;
            }
        }
    }
    return this;
}

console.log([6,4,0, 3,-2,1].BubbleSort());

Javascript Array bubbleSort()


Array.prototype.bubbleSort = function() {
 var flag= false;
 while(!flag){/* w ww.j a v a  2 s.  com*/
  flag=true;
  for (var i = 0; i < this.length-1; i++) {
  if (this[i] > this[i+1]){  
        var x = this[i+1];  
        this[i+1] = this[i];  
        this[i] = x;  
        flag = false;
  }
 }
}
 return this;

};
console.log([6,4,0, 3,-2,1].bubbleSort());



PreviousNext

Related