Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

/**/*  w w  w  .j  ava2  s .  c  o  m*/
 * Bubble Sort Javascript implementation
 *  - Best: n / Average: n^2 / Worst: n^2
 *  - Memory: 1 / Stable
 *
 * @author: Sebastian Kim
 */

Array.prototype.bubbleSort = function() {
  n = this.length;

  while (n > 0) {
    var newn = 0;
    for(var i = 1; i < n; i++) {
      if(this[i - 1] > this[i]) {
        // Swap
        tmp = this[i];
        this[i] = this[i - 1];
        this[i - 1] = tmp;

        newn = i;
      }
    }
    n = newn;
  }

  return this;
}

Related

  1. bubbleSort()
    var arr = [4,1,2,5,-3,1,2,5];
    console.log(arr);
    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 temp =this[i];
                    this[i] = this[j];
                    this[j] = temp;
    ...
    
  2. bubbleSort()
    var unsortedArray = [4, 3, 2, 7, 10, 6];
    Array.prototype.bubbleSort = function() {
      var swapElement;
      do {
        swapElement = false;
        for (var i = 1; i < this.length; i++){
          if (this[i-1] > this[i]){
            var x = this[i-1];
            this[i-1] = this[i];
    ...
    
  3. bubbleSort()
    Array.prototype.bubbleSort = function bubbleSort() {
      let temp;
      let sorted = [];
      this.forEach(function(el,i){
        sorted[i] = el;
      });
      for(let i=0;i<sorted.length;i++) {
        for(let j=0;j<sorted.length-i-1;j++) {
          if(sorted[j] > sorted[j+1]) {
    ...
    
  4. 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 temp = this[i+1];
            this[i+1] = this[i];
    ...
    
  5. bubbleSort()
    var unsortedArray = [4, 3, 2, 7, 10, 6];
    Array.prototype.bubbleSort = function() {
      var bool = true;
      do {
        bool = false;
        for (var i=0; i < unsortedArray.length; i++) {
          if (unsortedArray[i] > unsortedArray[i+1]) {
            var swapped = unsortedArray[i+1];
            unsortedArray[i+1] = unsortedArray[i];
    ...
    
  6. bubbleSort()
    Array.prototype.bubbleSort = function () {
      var i, j, temp;
      for (var i = 0; i < this.length; i++) {
        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;
    };
    var num = [22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70];
    num.bubble_sort();
    for (var i = 0; i < num.length; i++)
      document.body.innerHTML += num[i] + " ";
    
  7. bubbleSort()
    Array.prototype.bubbleSort = function() {
      let sorted = false;
      while (sorted === false) {
        sorted = true;
        for (var i = 0; i < this.length; i++) {
          let first = this[i];
          let second = this[i+1];
          if (first > second){
            this[i] = second;
    ...
    
  8. bubbleSort()
    Array.prototype.bubbleSort = function() {
      for(var i=0; i<this.length-1; i++) {
        for(var j=(i+1); j<this.length; j++) {
          if (this[i] > this[j]) {
            var temp = this[i];
            this[i] = this[j];
            this[j] = temp;
    };
    
  9. 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;
    ...