Merge Sort java with a few details please include comments i

Merge Sort java with a few details, please include comments if possible

Generate an array with 100 random integers between 0 and 1000 Print the original array to the screen Use your merge sort algorithm to sort the array from lowest to highest Print the sorted array to the screen

Solution

/* Class name MergeSortRandomNumber.java */

import java.util.Random;

public class MergeSortRandomNumber{

   private int[] array;
private int[] tempMergArr;
private int length;
   public static void main(String[] args) {

      
  
      
   int inputArr[] =new int[10];// Storing array elements default 10 we can modify depends on requirement
   Random r = new Random();//Create Object for Random Predefined class
   System.out.println(\"Random Numbers\");//Printing Random Numbers
   for(int i=0;i<10;i++)
   {
       int Low = 0;//Start from 0
       int High = 100;//Up to 100
       int Result = r.nextInt(High-Low) + Low;
      
       System.out.print(Result);//Prining on the console random numbers
System.out.print(\" \");
       inputArr[i]=Result;
   }
   System.out.println(\"\");
   System.out.println(\"Input Array\");//Printing input array on the console start
   for(int i=0;i<10;i++)
   {
       System.out.print(inputArr[i]);//
       System.out.print(\" \");
      
   }       //Printing input array on the console end
   System.out.println(\"\");
   System.out.println(\"After Merge sort from low to high\");
   //merge Sort Main logic started
   RandomNumber mms = new RandomNumber();
mms.sort(inputArr);//Passing our input array calling sort() method
for(int i:inputArr){
System.out.print(i);//printing Final values from low to high
System.out.print(\" \");
}


  
  
}
  
   public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);//Calling Merge sort main logic
}

private void doMergeSort(int lowerIndex, int higherIndex) {

if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
doMergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}
  
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
   //Compare elements at First and Second,
//and move smaller element at Merged array
  
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
  
}

}

=============================

Expected Out put

Random Numbers
19 95 97 32 86 36 48 34 46 71


Input Array
19 95 97 32 86 36 48 34 46 71


After Merge sort from low to high
19 32 34 36 46 48 71 86 95 97

Merge Sort java with a few details, please include comments if possible Generate an array with 100 random integers between 0 and 1000 Print the original array t
Merge Sort java with a few details, please include comments if possible Generate an array with 100 random integers between 0 and 1000 Print the original array t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site