From d641f23ae620f89400e31b8d1d3b8d6acda41dc4 Mon Sep 17 00:00:00 2001 From: koushithatadiboina Date: Tue, 25 Nov 2025 15:47:19 +0530 Subject: [PATCH] Create InsertionSort I added a simple and clean implementation of the Insertion Sort algorithm in JavaScript. Please review and merge. Thank you! --- InsertionSort | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 InsertionSort diff --git a/InsertionSort b/InsertionSort new file mode 100644 index 0000000000..ae1026de2c --- /dev/null +++ b/InsertionSort @@ -0,0 +1,14 @@ +function insertionSort(arr) { + for (let i = 1; i < arr.length; i++) { + let key = arr[i]; + let j = i - 1; + + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + return arr; +} +console.log(insertionSort([5, 2, 9, 1, 3]));