Javascript: Bubble Sort

31,866

Solution 1

Another bubble sort implementation:

const bubbleSort = array => {
  const arr = Array.from(array); // avoid side effects
  for (let i = 1; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }
  return arr;
};

Solution 2

Please look at the following sequence:

[5, 4, 3, 2, 1]

Now lets say you need to sort this in the ascending order using bubble sort.

So, you iterate the array and swap adjacent elements which are ordered otherwise.

Here is what you will get after the completion of the iteration

[4, 3, 2, 1, 5]

Now if you do this another time, you will get this:

[3, 2, 1, 4, 5]

Likewise, you need to repeat the iteration enough times to get it sorted fully. This means you need 2 nested loops. The inner loop is to iterate the array and the outer loop is to repeat the iteration.

Please see the step-by-step example of this article.

Solution 3

You need an inner loop to complete the sort correctly:

function bubble(arr) {
      var len = arr.length;
    
      for (var i = 0; i < len ; i++) {
        for(var j = 0 ; j < len - i - 1; j++){ // this was missing
        if (arr[j] > arr[j + 1]) {
          // swap
          var temp = arr[j];
          arr[j] = arr[j+1];
          arr[j + 1] = temp;
        }
       }
      }
      return arr;
    }

document.write(bubble([1,9,2,3,7,6,4,5,5]));

Solution 4

const bubbleSort = (array)=>{
  let sorted = false;

  let counter =0;

  while(!sorted){
    sorted = true;  
    for(let i =0; i < array.length -1 -counter; i++){

      if(array[i] > array[i+1]){
        helper(i,i+1,array);        
        sorted = false;
      }
    } 
    counter++;
  }
  return array;

}

//swap function
function helper(i,j, array){
  return [array[i],array[j]] = [array[j],array[i]]
}

let array=[8,5,2,9,5,6,3];

console.log(bubbleSort(array))

Solution 5

var array = [6,2,3,7,5,4,1];
function bubbleSort(arr) {
    for(let j=0;j<arr.length;j++) {
        for(let i = 0; i < arr.length; i++) {
            if(arr[i]>arr[i+1]) {
                var temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
            }
        }
    }      
    return arr;
}
console.log(bubbleSort(array));
Share:
31,866
Kabir Shah
Author by

Kabir Shah

I make things.

Updated on July 09, 2022

Comments

  • Kabir Shah
    Kabir Shah almost 2 years

    I have made a bubble sort algorithm (sorta) using JS. It works sometimes, but the problem is that it only iterates through the array once. Here is my code:

    function bubble(arr) {
      for (var i = 0; i < arr.length; i++) {
        if (arr[i] > arr[i + 1]) {
          var a = arr[i]
          var b = arr[i + 1]
          arr[i] = b
          arr[i + 1] = a
        }
      }
      return arr;
    }
    
  • Kabir Shah
    Kabir Shah almost 8 years
    Please explain the inner loop, why is it needed?
  • Giovanni Esposito
    Giovanni Esposito over 3 years
    A small explanation of what you have posted would be appriciated. Code-only answer discourages.
  • Xyz
    Xyz over 3 years
    Please feel free to explain what your code does, and specifically why it solves the issue at hand