Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit 9136695

Browse files
authored
add insertion sort (#907)
1 parent 4330f18 commit 9136695

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function insertionSort(arr) {
2+
for (let i = 1; i < arr.length; i++) {
3+
let currentValue = arr[i]
4+
let j
5+
for (j = i - 1; j >= 0 && arr[j] > currentValue; j--) {
6+
arr[j + 1] = arr[j]
7+
}
8+
arr[j + 1] = currentValue
9+
}
10+
return arr
11+
}
12+
console.log(insertionSort([2, 1, 3, 7, 5])) // [1, 2, 3, 5, 7]

0 commit comments

Comments
 (0)