public class MergeSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = { 12, 11, 13, 5, 6, 7,99,87,0,2,103 };
System.out.println("printing an array :::");
printArray(arr);
sort(arr,0,arr.length-1);
System.out.println("printing a sorted array :::");
printArray(arr);
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void sort(int arr[], int startIndex, int endIndex){
if(startIndex==endIndex)
return;
int midIndex = (startIndex+endIndex)/2;
sort(arr,startIndex, midIndex);
sort(arr,midIndex+1,endIndex);
merge(arr, startIndex, midIndex, endIndex);
}
public static void merge(int arr[], int startIndex, int midIndex, int endIndex ){
int size1 = midIndex-startIndex+1;
int size2 = endIndex-midIndex;
int firstPart[] = new int[size1];
int secondPart[] = new int[size2];
System.out.println("startIndex::"+startIndex+", midIndex::"+midIndex+", endIndex::"+endIndex);
System.out.println("size1 ::"+size1);
System.out.println("size2 ::"+size2);
int i=0,j=0,k=0;
//copy data into the array
for(i=0;i<size1;i++){
firstPart[i] = arr[startIndex+i];
}
for(j=0;j<size2;j++){
secondPart[j]=arr[j+midIndex+1];
}
i=0;
j=0;
k=startIndex;
System.out.println("firstPart::");
printArray(firstPart);
System.out.println("secondPart::");
printArray(secondPart);
while(i<size1 && j<size2){
if(firstPart[i]<secondPart[j]){
arr[k]=firstPart[i];
i++;
k++;
} else if(firstPart[i]>=secondPart[j]){
arr[k]=secondPart[j];
j++;
k++;
}
}
while(i<size1){
arr[k] = firstPart[i];
i++;
k++;
}
while(j<size2){
arr[k]=secondPart[j];
j++;
k++;
}
System.out.println("Merget Array::");
printArray(arr);
}
}
Output :
printing an array ::: 12 11 13 5 6 7 99 87 0 2 103 startIndex::0, midIndex::0, endIndex::1 size1 ::1 size2 ::1 firstPart:: 12 secondPart:: 11 Merget Array:: 11 12 13 5 6 7 99 87 0 2 103 startIndex::0, midIndex::1, endIndex::2 size1 ::2 size2 ::1 firstPart:: 11 12 secondPart:: 13 Merget Array:: 11 12 13 5 6 7 99 87 0 2 103 startIndex::3, midIndex::3, endIndex::4 size1 ::1 size2 ::1 firstPart:: 5 secondPart:: 6 Merget Array:: 11 12 13 5 6 7 99 87 0 2 103 startIndex::3, midIndex::4, endIndex::5 size1 ::2 size2 ::1 firstPart:: 5 6 secondPart:: 7 Merget Array:: 11 12 13 5 6 7 99 87 0 2 103 startIndex::0, midIndex::2, endIndex::5 size1 ::3 size2 ::3 firstPart:: 11 12 13 secondPart:: 5 6 7 Merget Array:: 5 6 7 11 12 13 99 87 0 2 103 startIndex::6, midIndex::6, endIndex::7 size1 ::1 size2 ::1 firstPart:: 99 secondPart:: 87 Merget Array:: 5 6 7 11 12 13 87 99 0 2 103 startIndex::6, midIndex::7, endIndex::8 size1 ::2 size2 ::1 firstPart:: 87 99 secondPart:: 0 Merget Array:: 5 6 7 11 12 13 0 87 99 2 103 startIndex::9, midIndex::9, endIndex::10 size1 ::1 size2 ::1 firstPart:: 2 secondPart:: 103 Merget Array:: 5 6 7 11 12 13 0 87 99 2 103 startIndex::6, midIndex::8, endIndex::10 size1 ::3 size2 ::2 firstPart:: 0 87 99 secondPart:: 2 103 Merget Array:: 5 6 7 11 12 13 0 2 87 99 103 startIndex::0, midIndex::5, endIndex::10 size1 ::6 size2 ::5 firstPart:: 5 6 7 11 12 13 secondPart:: 0 2 87 99 103 Merget Array:: 0 2 5 6 7 11 12 13 87 99 103 printing a sorted array ::: 0 2 5 6 7 11 12 13 87 99 103