From 7dd1831766543a0467ca0d696a345360c5f586c9 Mon Sep 17 00:00:00 2001 From: Abhishek Yadav Date: Sat, 9 Mar 2024 01:52:47 +0530 Subject: [PATCH] Insertion sort in an array in js completed. --- arr_ins_sort.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/arr_ins_sort.js b/arr_ins_sort.js index 3ec3dfa..d081272 100644 --- a/arr_ins_sort.js +++ b/arr_ins_sort.js @@ -2,3 +2,22 @@ const arr = [67,12,45,7,30,80,56,1,22]; +function insertionSort(arr){ + let n = arr.length; + let k; + + let i,j; + for(i = 0; i < n; i++){ + k = arr[i]; + j = i - 1; + for(j; j >= 0, arr[j]>k; --j){ + arr[j+1] = arr[j]; + } + arr[j+1] = k; + } +} + + +console.log("Original Array : ",arr); +insertionSort(arr); +console.log("Selection Array : ",arr); \ No newline at end of file