Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

// (for morning challenge) bubbleSort function that returns swap count 
function bubbleSort (arr) {

  var swapped = true;
  var swapCount = 0;

  while (swapped === true) {

    swapped = false;//from   w ww .  ja  va 2s. c  o m

    for (var i = 0; i < arr.length-1; i++) {

      var curr = arr[i];
      var next = arr[i+1];

      if (curr > next) {
        arr[i] = next;
        arr[i+1] = curr;
        swapCount += 1;
        swapped = true;
      }    
    }
  }

  this.arr = arr;
  return swapCount;
};


// bubbleSort func that returns the sorted array, and is available to all Arrays
Array.prototype.bubbleSort = function () {

  var swapped = true;

  while (swapped === true) {

    swapped = false;

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

      var curr = this[i];
      var next = this[i+1];

      if (curr > next) {
        this[i] = next;
        this[i+1] = curr;
        swapped = true;
      }    
    }
  }

  return this;
}

Related

  1. 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;
    ...
    
  2. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var secondArray = this.slice(0);
      for(var j = 0; j <= secondArray.length; j++) {
        for(var i = 0; i < secondArray.length-1; i++) {
          var first_el = secondArray[i];
          var second_el = secondArray[i+1];
          if(first_el > second_el) {
            secondArray[i] = second_el;
            secondArray[i+1] = first_el;
    ...
    
  3. bubbleSort()
    function factors(num) {
      for(let i=0; i <=num; i++ ) {
        if (num % i === 0) {
          console.log(i);
    Array.prototype.bubbleSort = function () {
      let sorted = false;
    ...
    
  4. bubbleSort()
    var fibonacci = function(num) {
      var fibs = [0, 1];
      if (num < 3) {
        return fibs.slice(0, num);
      while (fibs.length < num) {
        fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]);
      return fibs;
    ...
    
  5. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var temp;
      for(var i = 0; i < this.length - 1; i++) {
        for(var z = 0; z <= this.length - i - 1; z++) {
          if(this[z] > this[z + 1]) {
            temp = this[z + 1];
            this[z + 1] = this[z];
            this[z] = temp;
      return this;
    var a = [23,34,55,24,51,56,27,29];
    a.bubbleSort();
    console.log(a);
    
  6. bubbleSort()
    function bubbleSort(arr){
      var bubbledArr = arr;
      var isSwapped = false;
      var count = 0;
      while(!isSwapped) {
        isSwapped = false;
        var moves = 0;
        for(var i = 0; i < bubbledArr.length -1; i++){
          var curr = bubbledArr[i];
    ...
    
  7. 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;
    ...
    
  8. 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];
    ...
    
  9. 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);
    };
    ...