private void merge(int[] array, int startIndex, int splitIndex, int endIndex){
    	
    	int[] tmpArray = new int[endIndex - startIndex + 1]; //new temporary array for storing merged elements
    	int tmpIndex = 0; //index to iterate over temporary array

    	int i=startIndex, j=splitIndex+1;
    	while(i<=splitIndex || j<=endIndex){ //while there are any items in the array part
    		if(i <= splitIndex && j<=endIndex){ //of there are items in both left and right parts
    			if(array[i] < array[j]){ //if left part is smaller
    				tmpArray[tmpIndex++] = array[i++];
    			}
    			else{ //if right part is smaller
    				tmpArray[tmpIndex++] = array[j++];
    			}
    		}
    		else if(i <= splitIndex){ //if there are items only in left part
    			tmpArray[tmpIndex++] = array[i++];
    		}
    		else{ //if there are items only in right part
    			tmpArray[tmpIndex++] = array[j++];
    		}
    	}
    	
    	//copy the merged items from temporary array to the main array
    	for(i=startIndex, tmpIndex=0; tmpIndex<tmpArray.length; i++, tmpIndex++){
    		array[i] = tmpArray[tmpIndex];
    	}
    	
    	tmpArray = null;
    }