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.
functioninsertionSort(arr){// Write your code hereletcount=0;if(arr.length>1){constmid=Math.floor(arr.length/2);constleft=arr.slice(0,mid);constright=arr.slice(mid);count+=left.length>1?insertionSort(left):0;count+=right.length>1?insertionSort(right):0;leti=0;letj=0;letk=0;while(i<left.length&&j<right.length){if(left[i]<=right[j]){arr[k]=left[i];i++;}else{arr[k]=right[j];j++;count=count+(mid-i);}k++;}while(i<left.length){arr[k]=left[i];i++;k++;}while(j<right.length){arr[k]=right[j];j++;k++;}}returncount;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insertion Sort Advanced Analysis
You are viewing a single comment's thread. Return to all comments →
JS