public static void merge(int[] array, int start, int mid, int end) { int[] temp = new int[array.length]; int i=start,j=mid+1,k=start; while(i != mid+1 && j != end +1 ) { if(array[i] < array[j]) { temp[k++] = array[i++]; } else { temp[k++] = array[j++]; } } while( i != mid + 1) { temp[k++] = array[i++]; } while( j != end + 1) { temp[k++] = array[j++]; } for(i = start;i<=end;i++) { array[i] = temp[i]; } for(int p : array) { System.out.print( p + " "); } System.out.println(); } static void mergeSort(int[] array, int start, int end) { if(start < end) { int mid = (start + end) / 2; mergeSort(array,start,mid); mergeSort(array,mid+1,end); merge(array,start,mid,end); } } public static void main(String[] args) { int[] array = {100, 45, 47, 36, 21, 13, 7,0,123}; //System.out.println(Arrays.toString(array)); for(int i : array) { System.out.print( i + " "); } System.out.println(); mergeSort(array,0,array.length-1); }