private void merge(int[] theArray, int[] leftArray, int[] rightArray, SorterParams.SortDirection sortDir)
    {
        int i = 0;
        int l = 0;
        int r = 0;

        //merge smaller element into resulting array
        while(l < leftArray.length && r < rightArray.length)
        {
            if(sortDir == SorterParams.SortDirection.ASCENDING)
            {
                if(leftArray[l] < rightArray[r])
                {
                    theArray[i] = leftArray[l];
                    l++;
                }
                else
                {
                    theArray[i] = rightArray[r];
                    r++;
                }
            }
            else
            {
                if(leftArray[l] > rightArray[r])
                {
                    theArray[i] = leftArray[l];
                    l++;
                }
                else
                {
                    theArray[i] = rightArray[r];
                    r++;
                }
            }
            i++;
        }
        
        if(i < theArray.length)  //copy the remainder
        {
            for(int j = i; j < theArray.length; j++)
            {
                if(l < leftArray.length)
                {
                    theArray[j] = leftArray[l];
                    l++;
                }
                else
                {
                    theArray[j] = rightArray[r];
                    r++;
                }
            }
        }
    }