diff --git a/Sorts/InsertionSOrt.js b/Sorts/InsertionSOrt.js new file mode 100644 index 0000000..40254a4 --- /dev/null +++ b/Sorts/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; +}