Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

var unsortedArray = [4, 3, 2, 7, 10, 6];

Array.prototype.bubbleSort = function() {
  var swapElement;
  do {/*from w w  w  . j  av  a  2s  .  c o  m*/
    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];
        this[i] = x;
        swapElement = true;
      }
    }
  } while (swapElement);
  return this;
};

console.log(unsortedArray.bubbleSort());

Related

  1. 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]){
            this[i+1] = [this[i], this[i]=this[i+1]][0]
            sorted = false
    };
    var array = [3,6,4,7,9,2,8]
    array.bubbleSort();
    console.log(array);
    
  2. bubbleSort()
    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 t = this[i];
                    this[i] = this[j];
                    this[j] = t;
    };
    var a = [6,4,0, 3,-2,1];
    console.log(a);
    a.bubbleSort();
    console.log(a);
    
  3. bubbleSort()
    a = [21,3,23,7,3,2]
    Array.prototype.bubbleSort = function () {
      var sorted = false;
      do {
        sorted = true;
        for( var i = 0; i < this.length - 1; i++){
          if(this[i] > this[i+1]) {
            sorted = false;
            temp = this[i];
    ...
    
  4. bubbleSort()
    Array.prototype.bubbleSort = function() {
      let sorted = true;
      for(let i = 0; i + 1 < this.length; i++){
        if (this[i] > this[i+1]){
          let temp = this[i];
          this[i] = this[i + 1];
          this[i + 1] = temp;
          sorted = false;
      if (sorted){
        return this;
      } else {
        return this.bubbleSort();
    };
    console.log([1,6,3,8,6,5,10].bubbleSort()); 
    
  5. 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;
    ...
    
  6. 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]) {
    ...
    
  7. 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];
    ...
    
  8. 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];
    ...
    
  9. bubbleSort()
    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]) {
            tmp = this[i];
            this[i] = this[i - 1];
            this[i - 1] = tmp;
    ...