Skip to content

Commit 0b6004b

Browse files
Create BubbleSortAlgo-JS
I have added a simple Bubble Sort implementation in JavaScript. The code is clean, easy to understand, and follows the style of the repository. Please review and merge this PR.
1 parent 08d8c6b commit 0b6004b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

BubbleSortAlgo-JS

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function bubbleSort(arr) {
2+
let n = arr.length;
3+
for (let i = 0; i < n - 1; i++) {
4+
for (let j = 0; j < n - i - 1; j++) {
5+
if (arr[j] > arr[j + 1]) {
6+
// swap
7+
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
8+
}
9+
}
10+
}
11+
return arr;
12+
}
13+
14+
console.log(bubbleSort([5, 3, 8, 1, 2])); // Output: [1, 2, 3, 5, 8]

0 commit comments

Comments
 (0)