MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / merge

Method merge

CountInversions.java:26–67  ·  view source on GitHub ↗
(long arr[],long s,long m, long e)

Source from the content-addressed store, hash-verified

24 }
25
26 public static long merge(long arr[],long s,long m, long e)
27 {
28 int n1=(int)(m-s+1);//5
29 int n2=(int)(e-m);//5
30 long L[]=new long[n1];
31 long R[]=new long[n2];
32 long count=0;
33 // Creating a subarray!
34 for(int i=0;i<n1;i++)
35 {
36 L[i]=arr[(int)(s)+i];
37 }
38 for(int j=0;j<n2;j++)
39 {
40 R[j]=arr[(int)(m)+1+j];
41 }
42 int i=0,j=0,k=(int)(s);
43 while(i<n1 && j<n2)
44 {
45 if(L[i]<=R[j])
46 {
47 arr[k]=L[i];
48 i++;
49 }
50 else
51 {
52 arr[k]=R[j];
53 count+=n1-i;
54 j++;
55 }
56 k++;
57 }
58 while(i<n1)
59 {
60 arr[k++]=L[i++];
61 }
62 while(j<n2)
63 {
64 arr[k++]=R[j++];
65 }
66 return count;
67 }
68}

Callers 1

mergeSortMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected