Skip to content

Commit 5891f24

Browse files
author
polun
committed
add buble sort implementation code
1 parent ae6fad9 commit 5891f24

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

code/bubbleSort.js

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

0 commit comments

Comments
 (0)