Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

Array.prototype.bubbleSort = function() {
  let sorted = false;/*from  w  w  w  .  ja v  a  2s  . c  o m*/

  while (!sorted){
    sorted = true;

    for (let i = 0; i < this.length - 1; i++) {
      let j = i + 1;

      if (this[i] > this[j]) {
        sorted = false;
        [this[i], this[j]] = [this[j], this[i]];
      }
    }
  }

  return this;
};

// let a = [2, 4, 3, 9];
// console.log(a.bubbleSort());

Related

  1. bubbleSort()
    "use strict";
    Array.prototype.bubbleSort = function() {
      var sorted = false;
      var arr = this;
      var oneIteration = function() {
        for(var i = 0; i < arr.length - 1; i++) {
          var first = arr[i]
          var second = arr[i+1]
          if( first > second ) {
    ...
    
  2. bubbleSort()
    Array.prototype.bubbleSort = function() {
      while(true) {
        let doneSorting = true;
        for (i=0; i < this.length-1; i++) {
          if (this[i+1] < this[i]) {
            let tempI = this[i];
            this[i] = this[i+1];
            this[i+1] = tempI;
            doneSorting = false;
    ...
    
  3. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var sorted = false;
      while (!sorted) {
        sorted = true;
        for (var i = 0; i < this.length - 1; i++) {
          if (this[i + 1] < this[i]) {
            var current_item = this[i];
            this[i] = this[i + 1];
            this[i + 1] = current_item;
    ...
    
  4. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var isSorted = false;
      while (!isSorted) {
        isSorted = true;
        for (var i = 0; i < (this.length - 1); i++) {
          if (this[i] > this[i + 1]) {
            var tmp = this[i];
            this[i] = this[i + 1];
            this[i + 1] = tmp;
    ...
    
  5. bubbleSort()
    Array.prototype.bubbleSort = function () {
      let flag = false;
      while (flag === false) {
        flag = true;
        let i = 0;
        for (let j = i + 1; j < this.length; j++) {
          if (this[i] > this[j]) {
            flag = false;
            let holder = this[i];
    ...
    
  6. bubbleSort()
    "use strict";
    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]) {
            let tmp = this[i];
            this[i] = this[i + 1];
    ...
    
  7. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var unsorted = true;
      while (unsorted) {
        unsorted = false;
        for ( var i = 0; i < this.length - 1; i++ ) {
          if ( this[i] > this[i+1] ) {
            var placeholder = this[i];
            this[i] = this[i+1];
            this[i+1] = placeholder;
    ...
    
  8. 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;
    };
    console.log([5, 3, 4, 2, 1].bubbleSort());
    String.prototype.substrings = function () {
      let substrings = [];
      for (let start = 0; start < this.length; start++) {
        for (let len = 1; (start + len) <= this.length; len++) {
          substrings.push(this.substring(start, start + len));
      return substrings;
    };
    console.log("abc".substrings());
    
  9. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var unsorted = true;
      while (unsorted) {
        unsorted = false;
        for (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;
    ...