Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

'use strict';/*from w w w .  j a v a 2s  .c  o  m*/

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];
        this[i+1] = this[i];
        this[i] = temp;
      }
    }
  }

  return this;
};

console.log('--bubbleSort--');
let a = [6, 5, 3, 2, 3, 7];
console.log(a);
a.bubbleSort();
console.log(a);

Related

  1. bubbleSort()
    Array.prototype.bubbleSort = function (){
      let isSorted = false; 
      while(!isSorted){ 
        isSorted = true;
        for (var i = 0; i < this.length; i++){
          if (this[i] > this[i+1]){ 
            let earlierNum = this[i];
            this[i] = this[i + 1];
            this[i + 1] = earlierNum;
    ...
    
  2. 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];
    ...
    
  3. 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);
    };
    ...
    
  4. 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;
        });
    ...
    
  5. 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];
    ...
    
  6. bubble_sort()
    Array.prototype.bubble_sort = function() {
      var sorted = false
      var passes = 0
      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)
    ...
    
  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;
            var tmp = this[i - 1];
            this[i - 1] = this[i];
    ...
    
  8. 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;
    
  9. 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]) {
    ...