Bubble Sort Algorithm
A simple sorting algorithm with O(n²) time complexity, suitable for small datasets.
UnsortedSortedComparingSwapping
1function bubbleSort(arr) {
2 for(let i = 0; i < arr.length; i++) {
3 let noSwap = true
4 for(let j = 0; j < arr.length - 1 - i; j++) {
5 if(arr[j] > arr[j + 1]) {
6 [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
7 noSwap = false;
8 }
9 }
10 if(noSwap)break;
11 }
12
13 return arr
14}