Quick actions

cmd+k|ctrl+k

Navigation

Languages

BubbleSort in JavaScript

Snippet info

Language

JavaScript

Visibility

public

Author

patrykstachowiak

Created

2025-02-26T14:01:34.367427Z

Updated

2025-03-08T22:44:02.463942Z

const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];

function bubbleSort(array) {
  const length = array.length;
  for (let i = 0; i < length; i++) {
    for (let j = 0; j < length; j++) { 
      if(array[j] > array[j+1]) {
        //Swap the numbers
        let temp = array[j]
        array[j] = array[j+1];
        array[j+1] = temp;
      }
    }        
  }
}

bubbleSort(numbers);
console.log(numbers);
INFO