diff --git a/BubbleSortAlgo-JS b/BubbleSortAlgo-JS new file mode 100644 index 0000000000..fce3bbc26b --- /dev/null +++ b/BubbleSortAlgo-JS @@ -0,0 +1,14 @@ +function bubbleSort(arr) { + let n = arr.length; + for (let i = 0; i < n - 1; i++) { + for (let j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + // swap + [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; + } + } + } + return arr; +} + +console.log(bubbleSort([5, 3, 8, 1, 2])); // Output: [1, 2, 3, 5, 8]