diff --git a/InsertionSort.js b/InsertionSort.js new file mode 100644 index 0000000000..40254a4945 --- /dev/null +++ b/InsertionSort.js @@ -0,0 +1,15 @@ +function insertionSort(inputArr) { + let n = inputArr.length; + for (let i = 1; i < n; i++) { + // Choosing the first element in our unsorted subarray + let current = inputArr[i]; + // The last element of our sorted subarray + let j = i-1; + while ((j > -1) && (current < inputArr[j])) { + inputArr[j+1] = inputArr[j]; + j--; + } + inputArr[j+1] = current; + } + return inputArr; +}