Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

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

  while(sorted !== true) {
    sorted = true;//  w  w w .  j  av  a 2  s.  c o  m
    for(var i = 0; i < (this.length - 1); i++){
      var left = this[i];
      var right = this[i + 1];

      if(left > right){
        sorted = false;
        this[i] = right;
        this[i + 1] = left;
      }
    }
  }
  return this;
};


// console.log([5, 2, 7, 11].bubbleSort());


function substrings(string) {
  result = [];
  for(var start = 0; start < string.length; start++){
    for(var end = (start + 1); end <= string.length; end++){
      result.push(string.substring(start, end));
    }
  }
  return result;
}

// console.log(substrings("cat"));

Related

  1. 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]) {
            sorted = false;
            [this[i], this[i+1]] = [this[i+1], this[i]];
      return this;
    };
    
  2. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var sorted = false;
      while (!sorted) {
        sorted = true;
        for(var i = 0; i < this.length - 1; i++) {
          if (this[i] > this[i + 1]) {
            var temp = this[i];
            this[i] = this[i + 1];
            this[i + 1] = temp;
    ...
    
  3. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var sorted = false;
      while (!sorted) {
        sorted = true;
        for (var i = 0; i < this.length; i++) {
          if (this[i] > this[i + 1]) {
            sorted = false;
            var intermediate = this[i];
            this[i] = this[i + 1];
    ...
    
  4. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var sorted = false;
      do {
        sorted = true;
        for (var i = 0; i < this.length - 1; i++) {
          var j = i + 1;
          if (this[i] > this[j]) {
            var temp = this[i];
            this[i] = this[j];
    ...
    
  5. bubbleSort()
    Array.prototype.bubbleSort = function () {
      let isSorted = false;
      while (!isSorted) {
        isSorted = true;
        for (let i = 0; i < (this.length - 1); i++) {
          if (this[i] > this[i + 1]) {
            [this[i], this[i + 1]] = [this[i + 1], this[i]];
            isSorted = false;
      return this;
    };
    
  6. bubbleSort()
    Array.prototype.bubbleSort = function () {
        for (var i = 0; i < this.length - 1; i++) {
            var temp = null;
            for (var j = 0; j < this.length - 1 - i; j++) {
                if (this[j] > this[j + 1]) {
                    temp = this[j];
                    this[j] = this[j + 1];
                    this[j + 1] = temp;
        return this;
    };
    ary = [3, 2, 1];
    console.log(ary.bubbleSort());
    
  7. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var sorted = false;
      while (sorted === false) {
        sorted = true;
        for (var i = 0; i < this.length - 1; i++) {
          if (this[i] > this[i + 1]) {
            sorted = false;
            var tempNum = this[i];
            this[i] = this[i + 1];
    ...
    
  8. 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 dummy = this[i];
            this[i] = this[j];
            this[j] = dummy;
          };
        };
    ...
    
  9. bubbleSort()
    Array.prototype.bubbleSort = function () {
      for (var i = 0; i < this.length; i++) {
        for (var j = 0; j < this.length - 1; j++) {
          if (this[j] > this[j+1]){
            var placeholder = this[j+1]
            this[j+1] = this[j];
            this[j] = placeholder;
      return this;