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(arr:number[]):[number[],number]{leti=arr.length-1letx=arr[i]lets=0while(x!=0){lett=arr[i-1]??0if(t<x){arr[i]=xx=0}else{arr[i]=arr[i-1]i--s++if(t==x)s--}}return[arr,s]}constarrayCompare=<T>(arr1:T[],arr2:T[]):boolean=>{if(arr1.length!=arr2.length)returnfalse;returnarr1.every((e,i)=>e==arr2[i])}functionrunningTime(arr:number[]):number{/** * with small changes from previos answer, counting swaping attemp and sum em all */letswap_counter=0for(leti=1;i<arr.length;i++){let[sorted_part,swaped]=insertionSort1(arr.slice(0,i+1))letarr_sorted=[...sorted_part,...arr.slice(i+1)]if(!arrayCompare(arr,arr_sorted)){arr=arr_sortedswap_counter+=swaped}}returnswap_counter;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Running Time of Algorithms
You are viewing a single comment's thread. Return to all comments →
My answer in Typescript, simple