Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

Array.prototype.bubbleSort = function () {
  let flag = false;//  ww w.  j ava  2  s  .  c o  m

  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];
        this[i] = this[j];
        this[j] = holder;
      }

      i++;
    }
  }

  return this;
};

// console.log([3,4,2,5,1,1,3,4,5].bubbleSort());

String.prototype.substrings = function () {
  let substr = [];
  for (let i = 0; i < this.length; i++) {
    for (let j = i + 1; j <= this.length; j++) {
      if (!substr.includes(this.slice(i, j))){
        substr.push(this.slice(i, j));
      }
    }
  }
  return substr;
};

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

Related

  1. bubbleSort()
    Array.prototype.bubbleSort = function() {
      let sorted = false;
      while (sorted === false) {
        sorted = true;
        for (let i = 0; i < this.length - 1; i++) {
          let j = i + 1;
          if (this[i] > this[j]) {
            let first = this[i];
            let second = this[j];
    ...
    
  2. 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 ) {
    ...
    
  3. 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;
    ...
    
  4. 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;
    ...
    
  5. 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;
    ...
    
  6. bubbleSort()
    Array.prototype.bubbleSort = function() {
      let sorted = false;
      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]];
    ...
    
  7. 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];
    ...
    
  8. 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;
    ...
    
  9. 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());