Advertisement
uopspop

Untitled

Sep 13th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4.  
  5. public class MergeSort_Algo_iterative {
  6.     public static void main(String[] args)
  7.     {
  8. //        int[] ary = new int[]{20, 9, 3, 10, 300, 201, 11, 100, 77};
  9.         int[] ary = new int[]{20, 9, 3, 10, 88};
  10.  
  11.         int[] result = mergesort(ary);
  12.         printarray(result);
  13.     }
  14.  
  15.     private static int[] mergesort(int[] ary) {
  16.         Queue<int[]> myqueue = new LinkedList();
  17.         for (int i = 0; i < ary.length; i++) {
  18.             myqueue.add(new int[]{ary[i]});
  19.         }
  20.  
  21.         while(myqueue.size()>1){
  22.  
  23.             int[] aryLeft = myqueue.remove();
  24.             int[] aryRight = myqueue.remove();
  25.             System.out.printf("aryLeft size: %d - aryRight size: %d %n", aryLeft.length, aryRight.length);
  26.             int[] combinedAry = merge(aryLeft, aryRight);
  27.             myqueue.add(combinedAry);
  28.         }
  29.  
  30.         return myqueue.remove();
  31.     }
  32.  
  33.     // imagine you are at the last step of mergesort when you are coming from the bottom up.
  34.     // therefore, the array with size 1 is sorted; then up and the arrays with size 2 will be sorted too.
  35.     // here, we are simulating a up-k layers, and now we have two sorted sub-arrays to be merged.
  36.     private static int[] merge(int[] aryleftsorted, int[] aryrightsorted){
  37.         if (aryleftsorted.length == 0) return aryrightsorted;
  38.         if (aryrightsorted.length == 0) return aryleftsorted;
  39.  
  40.         int[] result = null;
  41.         if (aryleftsorted[0] <= aryrightsorted[0]) { // if smaller, extract it out in this layer
  42.             result = concat(subarray(aryleftsorted, 0, 1) , merge(subarray(aryleftsorted, 1, aryleftsorted.length), aryrightsorted)) ;
  43.         }else {
  44.             result = concat(subarray(aryrightsorted, 0, 1) , merge(aryleftsorted, subarray(aryrightsorted, 1, aryrightsorted.length))) ;
  45.         }
  46.         return result;
  47.     }
  48.  
  49.  
  50.     ///// util only below /////
  51.  
  52.     private static int[] concat(int[] first, int[] second){
  53.         int[] both = Arrays.copyOf(first, first.length + second.length);
  54.         System.arraycopy(second, 0, both, first.length, second.length);
  55.         return both;
  56.     }
  57.  
  58.     private static int[] copyarray(int[] array){
  59.         return subarray(array, 0, array.length);
  60.     }
  61.  
  62.     public static int[] subarray(int[] array, int beg, int end) {
  63.         return Arrays.copyOfRange(array, beg, end);
  64.     }
  65.  
  66.     private static void printarray(int[] ary){
  67.         for (int i = 0; i < ary.length; i++) {
  68.             System.out.printf("%4d" , ary[i]);
  69.         }
  70.         System.out.println();
  71.     }
  72.     private static void print2darray(int[][] ary){
  73.         for (int i = 0; i < ary.length; i++) {
  74.             for (int j = 0; j < ary[i].length; j++) {
  75.                 System.out.printf("%4d" , ary[i][j]);
  76.             }
  77.             System.out.println();
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement