private int[] merge(int[] leftArray, int[] rightArray) {
		int mergedLength = leftArray.length + rightArray.length;

		int[] mergedArray = new int[mergedLength];

		int leftIndex = 0;
		int rightIndex = 0;

		while (leftIndex + rightIndex < mergedLength) {
			// There are still items to be merged

			if (leftIndex < leftArray.length && rightIndex < rightArray.length) {
				// Both sublists have items remaining
				if (leftArray[leftIndex] <= rightArray[rightIndex]) {
					mergedArray[leftIndex + rightIndex] = leftArray[leftIndex];
					leftIndex++;
				} else {
					mergedArray[leftIndex + rightIndex] = rightArray[rightIndex];
					rightIndex++;
					inversions += leftArray.length - leftIndex;
				}
			}

			else if (leftIndex < leftArray.length) {
				// Only the left sublist has items remaining
				while (leftIndex < leftArray.length) {
					mergedArray[leftIndex + rightIndex] = leftArray[leftIndex];
					leftIndex++;
				}
			}

			else {
				// Only the right sublist has items remaining
				while (rightIndex < rightArray.length) {
					mergedArray[leftIndex + rightIndex] = rightArray[rightIndex];
					rightIndex++;
				}
			}
		}

		return mergedArray;
	}