Nodejs Array Bubble Sort bubblesort()

Here you can find the source of bubblesort()

Method Source Code

Array.prototype.bubblesort = function() {
  var done = false;
  while (! done) {
    done = true;//from w w w. j av  a2  s  .  co  m
    for (var i = 1; i < this.length; i++) {
      if (this[i - 1] > this[i]) {
        done = false;
        var tmp = this[i - 1];
        this[i - 1] = this[i];
        this[i] = tmp;
      }
    }
  }
  return this;
}

Related

  1. 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);
    };
    ...
    
  2. 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;
        });
    ...
    
  3. 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];
    ...
    
  4. bubbleSort()
    'use strict';
    Array.prototype.bubbleSort = function bubbleSort() {
      let sorted = false;
      while(!sorted) {
        sorted = true;
        for (let i = 0; i < this.length - 1; i++) {
          if (this[i] > this[i+1]) {
            sorted = false;
            let temp = this[i+1];
    ...
    
  5. bubble_sort()
    Array.prototype.bubble_sort = function() {
      var sorted = false
      var passes = 0
      var n = 0
      while(!sorted) {
        sorted = true
        for(var i = 1; i < this.length; i++) {
          if(this[i] < this[i-1]) {
            this.swap(i, i-1)
    ...
    
  6. bubblesort()
    Array.prototype.bubblesort = function() {
        var done = false;
        while (!done) {
            done = true;
            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;
    
  7. bubblesort()
    Array.prototype.bubblesort = function () {
        'use strict';
        var swap,
            i,
            lastIndex = this.length - 1;
        do {
            swap = false;
            for (i = 0; i < lastIndex; i++) {
                if (this[i] > this[i + 1]) {
    ...
    
  8. 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];
                    this[j] = temp;
        return this;
    console.log([6,4,0, 3,-2,1].BubbleSort());
    
  9. myBubbleSort()
    function myBubbleSort(arr) {
      var length = arr.length;
      for (var i = 0; i < length; i++) {
        for (var j = 0; j < (length - i - 1); j++) {
          if(arr[j] > arr[j+1]) {
            var tmp = arr[j];
            arr[j] = arr[j+1];
            arr[j+1] = tmp;
      return arr;
    Array.prototype.myBubbleSort = function () {
      var length = this.length;
      for (var i = 0; i < length; i++) {
        for (var j = 0; j < (length - i - 1); j++) {
          if(this[j] > this[j+1]) {
            var tmp = this[j];
            this[j] = this[j+1];
            this[j+1] = tmp;
      return this;
    };