Skip to content

Commit 5ef1951

Browse files
committed
Bubble sort
1 parent 95d3a7f commit 5ef1951

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

sort/bubbleSort.ts

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

0 commit comments

Comments
 (0)