From 39568772643b4032edfcf7274e8f46b241ae637c Mon Sep 17 00:00:00 2001 From: Coleman Scott IV Date: Mon, 28 Oct 2024 13:14:15 -0400 Subject: [PATCH] Added js insertionSort ans JavaScript folder --- JavaScript/insertionSort.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 JavaScript/insertionSort.js diff --git a/JavaScript/insertionSort.js b/JavaScript/insertionSort.js new file mode 100644 index 0000000..d10d5dc --- /dev/null +++ b/JavaScript/insertionSort.js @@ -0,0 +1,16 @@ +export function insertionSort(arr) { + for (let i = 1; i < arr.length; i++) { + let current = arr[i]; + + let j = 0; + + + for (j = i - 1; j >= 0 && arr[j] > current; j--) { + arr[j + 1] = arr[j]; + } + + arr[j + 1] = current; + } + + return arr; + } \ No newline at end of file