private static<T extends Comparable<? super T>>
    void merge(ArrayList<T> theArray, ArrayList<T> tempArray, int first, int mid, int last) {
        // initialize the local indexes to indicate the subarrays
        int first1 = first;          //beginning of first subarray
        int last1 = mid;             //end of first subarray
        int first2 = mid + 1;        //beginning of second subarray
        int last2 = last;            //end of second subarray
        // while both subarrays are not empty, copy the smaller item into the temporary array
        int index = first1;          // next available location in tempArray

        while((first1<= last1) && (first2 <= last2)) {
            // Invariant: tempArray:[first1..index-1] is in order
            //PA2: Set from less than to less than or equal to
            if(theArray.get(first1).compareTo(theArray.get(first2))<=0){
                tempArray.set(index, theArray.get(first1));
                //Else do add method
                first1++;
                //PA2, Increment counter
                counter++;
            }
            else{
                tempArray.set(index, theArray.get(first2));
                first2++;
            } //end if
            index++;
        } //end while

        // finish off the nonempty

        // finish off the first subarray, if necessary
        while(first1 <= last1) {
            // Invariant: tempArray[first1..index-1] is in order
            tempArray.set(index, theArray.get(first1));
            first1++;
            index++;
        } // end while

        // finish off the second subarray, if necessary
        while (first2 <= last2) {
            // Invariant: tempArray[first1..index-1] is in order
            tempArray.set(index, theArray.get(first2));
            first2++;
            index++;
        } // end while

        //copy results back into the original array
        for (index = first; index <= last; ++index){
            theArray.set(index, tempArray.get(index));
        } // end for
    }