From 675fee8bdc6e01bf014cce714d650b7f1098ec14 Mon Sep 17 00:00:00 2001 From: Aastha Bhasin <62609764+aastha-b@users.noreply.github.com> Date: Thu, 1 Oct 2020 21:59:02 +0530 Subject: [PATCH] Create InsertionSOrt.js --- Sorts/InsertionSOrt.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Sorts/InsertionSOrt.js 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; +}