Advertisement
uopspop

Untitled

Sep 13th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class MergeSort_Algo {
  4.     public static void main(String[] args)
  5.     {
  6.         int[] ary = new int[]{20, 9, 3, 10, 300, 201, 11, 100 };
  7.  
  8.         int[] result = mergeSort(ary);
  9.         printArray(result);
  10.     }
  11.  
  12.     private static int[] mergeSort(int[] ary) {
  13.         if (ary.length == 1) return ary;
  14.         int[] aryLeft = mergeSort(subArray(ary, 0, ary.length/2)); // split is done here in the input
  15.         int[] aryRight = mergeSort(subArray(ary, ary.length/2, ary.length)); // split is done here in the input
  16.         return merge(aryLeft, aryRight);
  17.     }
  18.  
  19.     // imagine you are at the last step of mergesort when you are coming from the bottom up.
  20.     // Therefore, the array with size 1 is sorted; then up and the arrays with size 2 will be sorted too.
  21.     // Here, we are simulating a up-k layers, and now we have two sorted sub-arrays to be merged.
  22.     private static int[] merge(int[] aryLeftSorted, int[] aryRightSorted){
  23.         if (aryLeftSorted.length == 0) return aryRightSorted;
  24.         if (aryRightSorted.length == 0) return aryLeftSorted;
  25.  
  26.         int[] result = null;
  27.         if (aryLeftSorted[0] <= aryRightSorted[0]) { // if smaller, extract it out in this layer
  28.             result = concat(subArray(aryLeftSorted, 0, 1) , merge(subArray(aryLeftSorted, 1, aryLeftSorted.length), aryRightSorted)) ;
  29.         }else {
  30.             result = concat(subArray(aryRightSorted, 0, 1) , merge(aryLeftSorted, subArray(aryRightSorted, 1, aryRightSorted.length))) ;
  31.         }
  32.         return result;
  33.     }
  34.  
  35.  
  36.     ///// Util Only below /////
  37.  
  38.     private static int[] concat(int[] first, int[] second){
  39.         int[] both = Arrays.copyOf(first, first.length + second.length);
  40.         System.arraycopy(second, 0, both, first.length, second.length);
  41.         return both;
  42.     }
  43.  
  44.     private static int[] copyArray(int[] array){
  45.         return subArray(array, 0, array.length);
  46.     }
  47.  
  48.     public static int[] subArray(int[] array, int beg, int end) {
  49.         return Arrays.copyOfRange(array, beg, end);
  50.     }
  51.  
  52.     private static void printArray(int[] ary){
  53.         for (int i = 0; i < ary.length; i++) {
  54.             System.out.printf("%4d" , ary[i]);
  55.         }
  56.         System.out.println();
  57.     }
  58.     private static void print2DArray(int[][] ary){
  59.         for (int i = 0; i < ary.length; i++) {
  60.             for (int j = 0; j < ary[i].length; j++) {
  61.                 System.out.printf("%4d" , ary[i][j]);
  62.             }
  63.             System.out.println();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement