Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sorting/InsertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

// Insertion sort runs in O(n²), or quadratic, time in the worst case.
// This typically isn’t very effective and should not be used for large lists.
// Because of insertion sort’s low hidden constant value, however,
// it usually outperforms more advanced algorithms such as quick sort or merge sort on smaller lists.


let insertionSort = (inputArr) => {
let length = inputArr.length;
for (let i = 1; i < length; i++) {
let key = inputArr[i];
let j = i - 1;

while (j >= 0 && inputArr[j] > key) {
inputArr[j + 1] = inputArr[j];
j = j - 1;
}
inputArr[j + 1] = key;
}
return inputArr;
};