package com.mergesort;

public class MergeSort {
    private int[] result;

    public int[] getResult() {
        return result;
    }

    public int[] sortArray(int[] array) {
        if (array.length < 2) return array;
        int middleIndex = array.length / 2;
        return mergeSort(sortArray(copyArray(array, 0, middleIndex - 1)),
                    sortArray(copyArray(array, middleIndex, array.length - 1)));
    }

    private int[] copyArray(int[] array, int low, int high) {
        int[] newArray = new int[high - low + 1];
        for (int i = low, j = 0; i <= high && j < newArray.length; i++, j++) {
            newArray[j] = array[i];
        }
        return newArray;
    }

    private int[] mergeSort(int[] a, int[] b) {
        int resultLength = a.length + b.length;
        result = new int[resultLength];
        int k = 0;
        int m = 0; //counters of indexes of input arrays

        for (int i = 0; i < resultLength; i++) {
            if (k < a.length && m < b.length) {
                if (a[k] < b[m]) {
                    result[i] = a[k];
                    k++;
                } else {
                    result[i] = b[m];
                    m++;
                }
            } else if (k < a.length) {
                result[i] = a[k];
                k++;
            } else {
                result[i] = b[m];
                m++;
            }
        }
        return result;
    }
}
@@ -46,180 +46,81 @@
 t %7B%0A
-    private int%5B%5D result;%0A%0A    public int%5B%5D getResult() %7B%0A        return result;%0A    %7D%0A%0A    public int%5B%5D sortArray(int%5B%5D array) %7B%0A        if (array.length %3C 2) return array
+%0A    public static void setArray(int%5B%5D array) %7B%0A        int leftIndex = 0
 ;%0A  
@@ -129,22 +129,21 @@
     int 
-middle
+right
 Index = 
@@ -159,11 +159,11 @@
 gth 
-/ 2
+- 1
 ;%0A  
@@ -172,152 +172,50 @@
     
-return mergeSort(sortArray(copyArray(array, 0, middleIndex - 1)),%0A                    sortArray(copyArray(array, middleIndex, array.length - 1))
+separateArray(array, leftIndex, rightIndex
 );%0A 
@@ -236,18 +236,28 @@
 ate 
-int%5B%5D copy
+static void separate
 Arra
@@ -280,20 +280,32 @@
 nt l
-ow
+eftIndex
 , int 
-h
+r
 igh
+tIndex
 ) %7B%0A
@@ -317,198 +317,293 @@
    i
-nt%5B%5D newArray = new int%5Bhigh - low + 1%5D;%0A        for (int i = low, j = 0; i %3C= high && j %3C newArray.length; i++, j++) %7B%0A            newArray%5Bj%5D = array%5Bi%5D;%0A        %7D%0A        return newArray;
+f (leftIndex %3C rightIndex) %7B%0A            int middleIndex = (leftIndex + rightIndex) / 2;%0A            separateArray(array, leftIndex, middleIndex);%0A            separateArray(array, middleIndex + 1, rightIndex);%0A            mergeSort(array, leftIndex, middleIndex, rightIndex);%0A        %7D
 %0A   
@@ -618,21 +618,27 @@
 private 
-int%5B%5D
+static void
  mergeSo
@@ -651,108 +651,216 @@
 %5B%5D a
+rray
 , int
-%5B%5D b) %7B%0A        int resultLength = a.length + b.length;%0A        result = new int%5BresultLength%5D;
+ leftIndex, int middleIndex, int rightIndex) %7B%0A        int%5B%5D buffer = new int%5Barray.length%5D;%0A        int leftLength = middleIndex - leftIndex + 1;%0A        int rightLength = rightIndex - middleIndex;%0A
 %0A   
@@ -965,15 +965,15 @@
 i %3C 
-resultL
+array.l
 engt
@@ -998,27 +998,29 @@
     if (k %3C 
-a.l
+leftL
 ength && m %3C
@@ -1024,11 +1024,14 @@
 m %3C 
-b.l
+rightL
 engt
@@ -1059,16 +1059,26 @@
 if (
-a
+buffer
 %5Bk%5D %3C b
+uffer
 %5Bm%5D)
@@ -1096,37 +1096,41 @@
             
-result%5Bi%5D = a
+array%5Bi%5D = buffer
 %5Bk%5D;%0A       
@@ -1188,37 +1188,41 @@
             
-result
+array
 %5Bi%5D = b
+uffer
 %5Bm%5D;%0A       
@@ -1288,11 +1288,13 @@
 k %3C 
-a.l
+leftL
 engt
@@ -1318,21 +1318,25 @@
     
-result%5Bi%5D = a
+array%5Bi%5D = buffer
 %5Bk%5D;
@@ -1398,21 +1398,25 @@
     
-result
+array
 %5Bi%5D = b
+uffer
 %5Bm%5D;
@@ -1465,31 +1465,8 @@
   %7D%0A
-        return result;%0A
     

fix class MergeSort