Skip to content

Commit f0f231e

Browse files
committed
added insertion_sort-javascript
1 parent 4442a1a commit f0f231e

File tree

1 file changed

+15
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)