public class test {
public static void main(String[] args) {
int[] randomArray = new int[100];
int[] randomArray2 = new int[100];
LoadArray(randomArray);
for (int i = 0; i < randomArray.length; i++) {
randomArray2[i] = randomArray[i];
}
System.out.println("Merge Sorted:");
mergeSort_srt(randomArray2,1,2);
PrintArray(randomArray2);
}
/*
* Loads the array with random numbers
*/
public static void LoadArray(int[] A) {
int arrayLength = A.length;
for (int i = 0; i < arrayLength; i++) {
A[i] = (int) (Math.random() * 100 + 1);
}
}
/*
* Prints the array called
*/
public static void PrintArray(int[] A) {
for (int i = 0; i < A.length; i++) {
System.out.print((A[i] + ","));
if ((i + 1) % 10 == 0) {
System.out.println("");
}
}
}
public static void mergeSort_srt(int array[],int lo, int n){
int low = lo;
int high = n;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
mergeSort_srt(array, low, middle);
mergeSort_srt(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((lo <= end_low) && (start_high <= high)) {
if (array[low] < array[start_high]) {
low++;
} else {
int Temp = array[start_high];
for (int k = start_high- 1; k >= low; k--) {
array[k+1] = array[k];
}
array[low] = Temp;
low++;
end_low++;
start_high++;
}
}
}
}