Skip to content

Commit d641f23

Browse files
Create InsertionSort
I added a simple and clean implementation of the Insertion Sort algorithm in JavaScript. Please review and merge. Thank you!
1 parent 08d8c6b commit d641f23

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

InsertionSort

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

0 commit comments

Comments
 (0)