Nodejs Array Bubble Sort bubbleSort()

Here you can find the source of bubbleSort()

Method Source Code

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];/*from w ww.j a v  a 2 s . c o m*/
                this[j] = t;
            }
        }
    }
};

var a = [6,4,0, 3,-2,1];

console.log(a);

a.bubbleSort();

console.log(a);

Related

  1. bubbleSort()
    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 temp = this[i];
            this[i] = this[i+1];
            this[i+1] = temp;
    ...
    
  2. bubbleSort()
    Array.prototype.bubbleSort = function(){
      var array = this
      var size = array.length
      var tmp
      for(var i = 0; i < size; i++)
        for(var j = 0; j < size; j++)
          if(array[i] > array[j]){
            tmp = array[j]
            array[j] = array[i]
    ...
    
  3. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var len = this.length
      var tmp
      for(var i = 1; i < len; i++) {
        for(var j = i; j > 0; j--) {
          if(this[j] < this[j-1]) {
            tmp = this[j]
            this[j] = this[j-1]
            this[j-1] = tmp
    ...
    
  4. bubbleSort()
    Array.prototype.bubbleSort = function() {
      var flag= false;
      while(!flag){
        flag=true;
        for (var i = 0; i < this.length-1; i++) {
        if (this[i] > this[i+1]){  
            var x = this[i+1];  
            this[i+1] = this[i];  
            this[i] = x;  
    ...
    
  5. 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);
    
  6. 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];
    ...
    
  7. 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()); 
    
  8. 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;
    ...
    
  9. 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];
    ...