Bubble sort an array with test case - Node.js Algorithm

Node.js examples for Algorithm:Sort

Description

Bubble sort an array with test case

Demo Code

a = [21,3,23,7,3,2]//w  w  w .j a v  a  2s.  com

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());

Related Tutorials