Nodejs Array Bubble Sort bubble_sort()

Here you can find the source of bubble_sort()

Method Source Code

Array.prototype.bubble_sort = function() {
  var sorted = false
  var passes = 0//w  w w  . ja v a  2s.  c  o  m
  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)
        sorted = false
      }
      n++
    }
    passes++
    print('  After pass '+passes+', n = '+n+', items = '+this)
  }
}

var cases = {
  sorted: range(1, 10),
  random: range(1, 10).randomize(),
  reversed: range(1, 10).reverse()
}

for(var c in cases) {
  print(c+' case')
  cases[c].bubble_sort()
  print('')
}

Related

  1. 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];
    ...
    
  2. 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);
    };
    ...
    
  3. 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;
        });
    ...
    
  4. 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];
    ...
    
  5. 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];
    ...
    
  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;
            var tmp = this[i - 1];
            this[i - 1] = this[i];
    ...
    
  7. 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;
    
  8. 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]) {
    ...
    
  9. 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());