We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functioninsertionSort1(n:number,arr:number[]):void{/** * simple as question requested. * * + number [i] store index of arr * + number [x] store sorting element * * index [i] move from end of [arr] to 0, check previous element [i - 1] is * lower than [x] then replace [arr][i] by [x] then stop * orelse keep moving [index] replace current [arr][i] by [i - 1] */leti=arr.length-1letx=arr[i]while(x!=0){if((arr[i-1]??0)<x){arr[i]=xx=0}else{arr[i]=arr[i-1]i--}console.log(arr.join(' '))}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insertion Sort - Part 1
You are viewing a single comment's thread. Return to all comments →
My answer in Typescript, simple, not minimized