Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

Array.prototype.bubbleSort = function() {
  while(true) {/*from  w w  w  .  j a v a 2s .co m*/
    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;
      }
    }
    if (doneSorting) {
      break;
    }
  }
}

// let x = [8, 12, 7, 2]
// x.bubbleSort()
// console.log(x)

String.prototype.substrings = function () {
  let result = [];
  let strArr = this.split("");

  for (i = 0; i < strArr.length; i++) {
    for (j = i+1; j < strArr.length+1; j++) {
      result.push(strArr.slice(i, j).join(""));
    }
  }

  return result;
}

let fruits = 'apples';
console.log(fruits.substrings());

Related

  1. 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;
    
  2. 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]) {
            var temp = this[i];
            this[i] = this[i+1];
            this[i+1] = temp;
    ...
    
  3. bubbleSort()
    "use strict";
    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]) {
            let tmp = this[i];
            this[i] = this[i + 1];
    ...
    
  4. 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];
    ...
    
  5. 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 ) {
    ...
    
  6. 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;
    ...
    
  7. 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;
    ...
    
  8. 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];
    ...
    
  9. 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]];
    ...