Bubble Sort

Sort an array using the bubble sort algorithm by repeatedly swapping adjacent elements.

Code

Algorithms
const a = [...arr];
for (let i = 0; i < a.length; i++) {
  for (let j = 0; j < a.length - i - 1; j++) {
    if (a[j] > a[j + 1]) [a[j], a[j + 1]] = [a[j + 1], a[j]];
  }
}
return a;

Parameters

Array to sort

Browser·fetch() may be limited by CORS

More JavaScript Snippets