diff --git a/Algorithms/InsertionSort.js b/Algorithms/InsertionSort.js new file mode 100644 index 0000000..afcfb66 --- /dev/null +++ b/Algorithms/InsertionSort.js @@ -0,0 +1,20 @@ +function insertionSort(arr) { + const n = arr.length; + + for (let i = 1; i < n; i++) { + let currentElement = arr[i]; + let j = i - 1; + + while (j >= 0 && arr[j] > currentElement) { + arr[j + 1] = arr[j]; + j--; + } + + arr[j + 1] = currentElement; + } +} + +// Example usage: +var arr = [7, 2, 1, 6, 5, 3, 8, 4]; +insertionSort(arr); +console.log(arr.toString());