Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

function bubbleSort(){
  var moves = 0;// w w w.ja  va  2 s.  co  m
  if(typeof this !== typeof []){
    throw  new TypeError('argument is not a array');
  }
  var length = this.length;
  for(var j = 0; j < this.length; j++){
     for(var i = 0; i < length; i++){
      if(isNaN(this[i])){
        throw new TypeError('array has no numbers');
      }
      if(this[i]  > this[i + 1]){
        var swapValue = this[i];
        this[i] = this[i + 1];
        this[i + 1] = swapValue;
        moves++;
        if(isNaN(this[i])){
          throw new TypeError('array has no numbers');
        }
        var newDiv = document.createElement('div');
        var newContent = document.createTextNode(this);
        newDiv.appendChild(newContent);
        var currentDiv = document.getElementById("div1");
        document.body.insertBefore(newDiv, currentDiv);
      }
    }
    length--;
  }
  return moves;
}
var numbers = [6,7,1,9,5,2,3,8,4];
Array.prototype.bubbleSort = bubbleSort;

Related

  1. bubble()
    'use strict';
    Array.prototype.bubble = function (){
      var i = this.length,
        tem,
        j;
      while (i>0){
        for (j=0; j < i -1; j++ ){
          if (this[j] > this[j+1]){
            tem = this[j];
    ...
    
  2. bubbleSort()
    Array.prototype.bubbleSort = function() {
      let sorted = false;
      while (!sorted) {
        sorted = true;
        for(let i = 0; i < this.length-1; i++) {
          if (this[i]>this[i+1]) {
            let temp = this[i];
            this[i] = this[i+1];
            this[i+1] = temp;
    ...
    
  3. bubbleSort()
    Array.prototype.bubbleSort = function(){
      var array = this
      var size = array.length
      var tmp
      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]
    ...
    
  4. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var len = this.length
      var tmp
      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
    ...