package com.janosgyerik.utils.algorithm.sort;

public class MergeSort {

    private MergeSort() {
        // utility class, forbidden constructor
    }

    public static void sort(int[] arr) {
        mergeSort(arr, 0, arr.length);
    }

    protected static void mergeSort(int[] arr, int from, int to) {
        int diff = to - from;
        if (diff < 2) {
            return;
        }
        int mid = from + diff / 2;
        mergeSort(arr, from, mid);
        mergeSort(arr, mid, to);
        merge(arr, from, mid, to);
    }

    protected static void merge(int[] arr, int from, int mid, int to) {
        int[] sorted = new int[to - from];
        for (int i = 0, pos1 = from, pos2 = mid; i < sorted.length; ++i) {
            if (pos1 < mid && (pos2 >= to || arr[pos1] <= arr[pos2])) {
                sorted[i] = arr[pos1++];
            } else {
                sorted[i] = arr[pos2++];
            }
        }
        System.arraycopy(sorted, 0, arr, from, sorted.length);
    }
}

@@ -530,34 +530,24 @@
 %0A    %7D%0A%0A    
-protected 
 static void 
@@ -647,13 +647,8 @@
     
-for (
 int 
@@ -656,60 +656,59 @@
  = 0
-, pos1 = from, pos2 = mid; i %3C sorted.length; ++i) %7B
+;%0A        int pos1 = from;%0A        int pos2 = mid;%0A
 %0A   
@@ -712,22 +712,21 @@
         
-    if
+while
  (pos1 %3C
@@ -737,23 +737,37 @@
  && 
-(
 pos2 
-%3E= to %7C%7C 
+%3C to) %7B%0A            if (
 arr%5B
@@ -785,17 +785,16 @@
 r%5Bpos2%5D)
-)
  %7B%0A     
@@ -804,32 +804,34 @@
         sorted%5Bi
+++
 %5D = arr%5Bpos1++%5D;
@@ -876,16 +876,18 @@
 sorted%5Bi
+++
 %5D = arr%5B
@@ -911,32 +911,187 @@
     %7D%0A        %7D%0A
+        while (pos1 %3C mid) %7B%0A            sorted%5Bi++%5D = arr%5Bpos1++%5D;%0A        %7D%0A        while (pos2 %3C to) %7B%0A            sorted%5Bi++%5D = arr%5Bpos2++%5D;%0A        %7D%0A
         System.a

Refactor mergeSort to more readable form