"How many inverted pairs" - that usually ends up with MergeSort solution (of course there are other solutions out there) defmergeSort(arr):iflen(arr) == 1:return0, arr mid= len(arr) // 2cnt1, arr1=mergeSort(arr[:mid]) cnt2, arr2=mergeSort(arr[mid:])#ret =[] cnt=0 i=0...
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, arrays may be too large for us to wait around for insertion sort to finish. Is there some other way we can calculate the number of times Insertion Sort shifts each elements when sorting an array?
Insertion Sort 1importjava.io.*;2importjava.util.*;34publicclassSolution {56publicstaticvoidinsertionSort(int[] ar)7{8intshifts = 0;9for(inti = 1;i<ar.length;i++){10inttemp =ar[i];11intj = i-1;12for(;j>=0 && ar[j]>temp;j--){13ar[j+1] =ar[j];14shifts++;15}16ar[j...