--- /dev/null
+/**\r
+ * 4-way split version of merge sort\r
+ */\r
+\r
+public class MergeSort4 extends MergeSort {\r
+\r
+ public static void main(String[] args) {\r
+ int problemSize= 1048576;\r
+ if(args.length>0){\r
+ problemSize=Integer.parseInt(args[0]);\r
+ }\r
+ MergeSort4 sort = new MergeSort4();\r
+ sort.run(problemSize);\r
+ }\r
+ \r
+ public MergeSort4(){\r
+ super();\r
+ }\r
+ \r
+ public void serializedSort(int A[]) {\r
+\r
+ if (A.length <= QUICK_SIZE) {\r
+ quickSort(A, 0, A.length - 1);\r
+ } else {\r
+\r
+ int q = A.length / 4;\r
+\r
+ int idxs0 = q;\r
+ int idxs1 = 2 * q;\r
+ int idxs2 = 3 * q;\r
+\r
+ int size0 = idxs0;\r
+ int size1 = idxs1 - idxs0;\r
+ int size2 = idxs2 - idxs1;\r
+ int size3 = A.length - idxs2;\r
+\r
+ int[] A_quarters0 = new int[size0];\r
+ int[] A_quarters1 = new int[size1];\r
+ int[] A_quarters2 = new int[size2];\r
+ int[] A_quarters3 = new int[size3];\r
+\r
+ for (int i = 0; i < idxs0; i++) {\r
+ A_quarters0[i] = A[i];\r
+ }\r
+ for (int i = idxs0; i < idxs1; i++) {\r
+ A_quarters1[i - idxs0] = A[i];\r
+ }\r
+ for (int i = idxs1; i < idxs2; i++) {\r
+ A_quarters2[i - idxs1] = A[i];\r
+ }\r
+ int amax=A.length;\r
+ for (int i = idxs2; i < amax; i++) {\r
+ A_quarters3[i - idxs2] = A[i];\r
+ }\r
+\r
+ int h1 = A_quarters0.length + A_quarters1.length;\r
+ int h2 = A_quarters2.length + A_quarters3.length;\r
+ int[] B_halves0 = new int[h1];\r
+ int[] B_halves1 = new int[h2];\r
+\r
+ serializedSort(A_quarters0);\r
+ serializedSort(A_quarters1);\r
+ serializedSort(A_quarters2);\r
+ serializedSort(A_quarters3);\r
+\r
+ sequentialMerge(A_quarters0, A_quarters1, B_halves0);\r
+ sequentialMerge(A_quarters2, A_quarters3, B_halves1);\r
+ sequentialMerge(B_halves0, B_halves1, A);\r
+\r
+ }\r
+\r
+ }\r
+\r
+ public void sort(int A[]) {\r
+ \r
+ if(A.length<=SERIALIZED_CUT_OFF){\r
+ serializedSort(A);\r
+ }else{\r
+ if (A.length <= QUICK_SIZE) {\r
+ quickSort(A,0,A.length-1);\r
+ } else {\r
+ \r
+ int q = A.length / 4;\r
+ \r
+ int idxs0 = q;\r
+ int idxs1 = 2 * q;\r
+ int idxs2 = 3 * q;\r
+ \r
+ int size0 = idxs0;\r
+ int size1 = idxs1 - idxs0;\r
+ int size2 = idxs2 - idxs1;\r
+ int size3 = A.length - idxs2;\r
+ \r
+ int[] A_quarters0 = new int[size0];\r
+ int[] A_quarters1 = new int[size1];\r
+ int[] A_quarters2 = new int[size2];\r
+ int[] A_quarters3 = new int[size3];\r
+ \r
+ for (int i = 0; i < idxs0; i++) {\r
+ A_quarters0[i] = A[i];\r
+ }\r
+ for (int i = idxs0; i < idxs1; i++) {\r
+ A_quarters1[i - idxs0] = A[i];\r
+ }\r
+ for (int i = idxs1; i < idxs2; i++) {\r
+ A_quarters2[i - idxs1] = A[i];\r
+ }\r
+ int amax=A.length;\r
+ for (int i = idxs2; i < amax; i++) {\r
+ A_quarters3[i - idxs2] = A[i];\r
+ }\r
+\r
+ int h1 = A_quarters0.length+A_quarters1.length;\r
+ int h2 = A_quarters2.length+A_quarters3.length;\r
+ \r
+ sese p1{\r
+ sort(A_quarters0);\r
+ }\r
+ sese p2{\r
+ sort(A_quarters1);\r
+ }\r
+ sese p3{\r
+ sort(A_quarters2);\r
+ }\r
+ //don't spawn off sese for last one...\r
+ sort(A_quarters3);\r
+ \r
+ merge(A_quarters0, A_quarters1, A_quarters2, A_quartes3, A);\r
+ }\r
+ }\r
+ }\r
+\r
+ public static void merge(int []a1, int []a2, int []a3, int[] a4, int[] a) {\r
+ int i1=0;\r
+ int i2=0;\r
+ int i3=0;\r
+ int i4=0;\r
+ int alength=a.length;\r
+ int v1=a1[0];\r
+ int v2=a2[0];\r
+ int v3=a3[0];\r
+ int v4=a4[0];\r
+ int v1m=a1.length;\r
+ int v2m=a2.length;\r
+ int v3m=a3.length;\r
+ int v4m=a4.length;\r
+ for(int i=0;i<alength;i++) {\r
+ if (v1<v2) {\r
+ if (v1<v3) {\r
+ if (v1<v4) {\r
+ a[i]=v1;\r
+ //v1 smallest\r
+ if (i1<v1m) {\r
+ v1=a1[i1++];\r
+ } else {\r
+ v1=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smalles\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ } else {\r
+ if (v3<v4) {\r
+ //v3 smallest\r
+ if (i3<v3m) {\r
+ v3=a3[i3++];\r
+ } else {\r
+ v3=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smallest\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ if (v2<v3) {\r
+ if (v2<v4) {\r
+ //v2 smallest\r
+ if (i2<v2m) {\r
+ v2=a2[i2++];\r
+ } else {\r
+ v2=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smallest\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ } else {\r
+ if (v3<v4) {\r
+ //v3 smallest\r
+ if (i3<v3m) {\r
+ v3=a3[i3++];\r
+ } else {\r
+ v3=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smallest\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
\r
int q = A.length / 4;\r
\r
- int[] idxs = new int[3];\r
- idxs[0] = q;\r
- idxs[1] = 2 * q;\r
- idxs[2] = 3 * q;\r
+ int idxs0 = q;\r
+ int idxs1 = 2 * q;\r
+ int idxs2 = 3 * q;\r
\r
- int size0 = idxs[0];\r
- int size1 = idxs[1] - idxs[0];\r
- int size2 = idxs[2] - idxs[1];\r
- int size3 = A.length - idxs[2];\r
+ int size0 = idxs0;\r
+ int size1 = idxs1 - idxs0;\r
+ int size2 = idxs2 - idxs1;\r
+ int size3 = A.length - idxs2;\r
\r
int[] A_quarters0 = new int[size0];\r
int[] A_quarters1 = new int[size1];\r
int[] A_quarters2 = new int[size2];\r
int[] A_quarters3 = new int[size3];\r
\r
- for (int i = 0; i < idxs[0]; i++) {\r
+ for (int i = 0; i < idxs0; i++) {\r
A_quarters0[i] = A[i];\r
}\r
- for (int i = idxs[0]; i < idxs[1]; i++) {\r
- A_quarters1[i - idxs[0]] = A[i];\r
+ for (int i = idxs0; i < idxs1; i++) {\r
+ A_quarters1[i - idxs0] = A[i];\r
}\r
- for (int i = idxs[1]; i < idxs[2]; i++) {\r
- A_quarters2[i - idxs[1]] = A[i];\r
+ for (int i = idxs1; i < idxs2; i++) {\r
+ A_quarters2[i - idxs1] = A[i];\r
}\r
- for (int i = idxs[2]; i < A.length; i++) {\r
- A_quarters3[i - idxs[2]] = A[i];\r
+ int amax=A.length;\r
+ for (int i = idxs2; i < amax; i++) {\r
+ A_quarters3[i - idxs2] = A[i];\r
}\r
\r
int h1 = A_quarters0.length + A_quarters1.length;\r
\r
int q = A.length / 4;\r
\r
- int[] idxs = new int[3];\r
- idxs[0] = q;\r
- idxs[1] = 2 * q;\r
- idxs[2] = 3 * q;\r
+ int idxs0 = q;\r
+ int idxs1 = 2 * q;\r
+ int idxs2 = 3 * q;\r
\r
- int size0 = idxs[0];\r
- int size1 = idxs[1] - idxs[0];\r
- int size2 = idxs[2] - idxs[1];\r
- int size3 = A.length - idxs[2];\r
+ int size0 = idxs0;\r
+ int size1 = idxs1 - idxs0;\r
+ int size2 = idxs2 - idxs1;\r
+ int size3 = A.length - idxs2;\r
\r
int[] A_quarters0 = new int[size0];\r
int[] A_quarters1 = new int[size1];\r
int[] A_quarters2 = new int[size2];\r
int[] A_quarters3 = new int[size3];\r
\r
- for (int i = 0; i < idxs[0]; i++) {\r
+ for (int i = 0; i < idxs0; i++) {\r
A_quarters0[i] = A[i];\r
}\r
- for (int i = idxs[0]; i < idxs[1]; i++) {\r
- A_quarters1[i - idxs[0]] = A[i];\r
+ for (int i = idxs0; i < idxs1; i++) {\r
+ A_quarters1[i - idxs0] = A[i];\r
}\r
- for (int i = idxs[1]; i < idxs[2]; i++) {\r
- A_quarters2[i - idxs[1]] = A[i];\r
+ for (int i = idxs1; i < idxs2; i++) {\r
+ A_quarters2[i - idxs1] = A[i];\r
}\r
- for (int i = idxs[2]; i < A.length; i++) {\r
- A_quarters3[i - idxs[2]] = A[i];\r
+ int amax=A.length;\r
+ for (int i = idxs2; i < amax; i++) {\r
+ A_quarters3[i - idxs2] = A[i];\r
}\r
\r
int h1 = A_quarters0.length+A_quarters1.length;\r
int h2 = A_quarters2.length+A_quarters3.length;\r
- int[] B_halves0 = new int[h1];\r
- int[] B_halves1 = new int[h2];\r
\r
sese p1{\r
sort(A_quarters0);\r
sese p3{\r
sort(A_quarters2);\r
}\r
- sese p4{\r
- sort(A_quarters3);\r
- }\r
+ //don't spawn off sese for last one...\r
+ sort(A_quarters3);\r
\r
- sese m1{\r
- merge(A_quarters0, A_quarters1, B_halves0);\r
- }\r
- \r
- sese m2{\r
- merge(A_quarters2, A_quarters3, B_halves1);\r
- }\r
- \r
- sese m3{\r
- merge(B_halves0, B_halves1, A);\r
- }\r
+ merge(A_quarters0, A_quarters1, A_quarters2, A_quartes3, A);\r
}\r
}\r
}\r
\r
+ public static void merge(int []a1, int []a2, int []a3, int[] a4, int[] a) {\r
+ int i1=0;\r
+ int i2=0;\r
+ int i3=0;\r
+ int i4=0;\r
+ int alength=a.length;\r
+ int v1=a1[0];\r
+ int v2=a2[0];\r
+ int v3=a3[0];\r
+ int v4=a4[0];\r
+ int v1m=a1.length;\r
+ int v2m=a2.length;\r
+ int v3m=a3.length;\r
+ int v4m=a4.length;\r
+ for(int i=0;i<alength;i++) {\r
+ if (v1<v2) {\r
+ if (v1<v3) {\r
+ if (v1<v4) {\r
+ a[i]=v1;\r
+ //v1 smallest\r
+ if (i1<v1m) {\r
+ v1=a1[i1++];\r
+ } else {\r
+ v1=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smalles\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ } else {\r
+ if (v3<v4) {\r
+ //v3 smallest\r
+ if (i3<v3m) {\r
+ v3=a3[i3++];\r
+ } else {\r
+ v3=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smallest\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ if (v2<v3) {\r
+ if (v2<v4) {\r
+ //v2 smallest\r
+ if (i2<v2m) {\r
+ v2=a2[i2++];\r
+ } else {\r
+ v2=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smallest\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ } else {\r
+ if (v3<v4) {\r
+ //v3 smallest\r
+ if (i3<v3m) {\r
+ v3=a3[i3++];\r
+ } else {\r
+ v3=2147483647;\r
+ }\r
+ } else {\r
+ //v4 smallest\r
+ if (i4<v4m) {\r
+ v4=a4[i4++];\r
+ } else {\r
+ v4=2147483647;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
}\r