Skip to content

Commit a492618

Browse files
Merge pull request akshitagit#32 from aniruddhparwal/master
Insertion Sort Added
2 parents 09ce297 + 92b6499 commit a492618

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Sorts/InserstionSort.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var Array = [96, 5, 42, 1, 6, 37, 21] // Function - Insertion Sort Algo.
2+
function insertionSort(unsortedArray) {
3+
for (let i = 1; i < unsortedArray.length; i++) {
4+
let current = unsortedArray[i];
5+
let j;
6+
for (j = i - 1; j >= 0 && unsortedArray[j] > current; j--) {
7+
unsortedArray[j + 1] = unsortedArray[j]
8+
}
9+
unsortedArray[j + 1] = current;
10+
}
11+
return unsortedArray;
12+
}
13+
// print sorted array
14+
console.log(insertionSort(Array));

0 commit comments

Comments
 (0)