Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.84 KB | None | 0 0
  1. package mergeSort;
  2.  
  3. public class merge {
  4.         static int zaehler = 0;
  5.        
  6.        
  7.     public static int[] combine(int[] a, int[] b) {
  8.         int[] c = new int[a.length + b.length];
  9.         int i_a = 0;
  10.         int i_b = 0;
  11.        
  12.         for (int i = 0; i < c.length; i++) {
  13.             if (i_b < b.length && i_a < a.length) {
  14.                 if (a[i_a] <= b[i_b]) {    
  15.                     c[i] = a[i_a];
  16.                     i_a++;                 
  17.                 }
  18.                 else {
  19.                     c[i] = b[i_b];
  20.                     i_b++;                 
  21.                 }
  22.             }
  23.             else if (i_b < b.length ) {
  24.                 c[i] = b[i_b];
  25.                 i_b++;
  26.             }
  27.             else {
  28.                 c[i] = a[i_a];
  29.                 i_a++;
  30.             }
  31.         }
  32.         return c;
  33.     }
  34.    
  35.    
  36.     public static int[] split(int[] c) {
  37.         printArray(c);
  38.        
  39.         if (c.length == 0 || c.length == 1) {
  40.             return c;
  41.         }
  42.         if (c.length == 2) {
  43.             if (istSortiert(c) == true) {
  44.                 return c;
  45.             }
  46.             else {
  47.                 invertiere(c);
  48.                 return c;
  49.             }
  50.         }
  51.         int m = c.length / 2;
  52.        
  53.         int[] a = new int[m];
  54.         int[] b = new int[c.length - m];
  55.        
  56.        
  57.         for (int i = 0; i < a.length; i++) {
  58.             if (c[i] != 0 ) {
  59.                 a[i] = c[i];
  60.                
  61.             }
  62.         }
  63.         for (int i = 0; i < b.length; i++) {
  64.             if (c[i] != 0) {
  65.                 b[i] = c[m + i];
  66.             }
  67.         }
  68.        
  69.         printArray(a);
  70.         printArray(b);
  71.         int[] d = combine(split(a), split(b));
  72.         return d;
  73.     }
  74.    
  75.  
  76.     public static boolean istSortiert(int[] a) {
  77.         if (a.length == 1) {
  78.             return true;
  79.         }
  80.         if(a[0] <= a[1]) {
  81.             return true;
  82.         }
  83.         else {
  84.             return false;
  85.         }
  86.     }
  87.    
  88.     public static int[] invertiere(int[] a) {
  89.         int z = a[0];
  90.         a[0] = a[1];
  91.         a[1] = z;
  92.         return a;
  93.     }
  94.    
  95.     public static void printArray(int[] a) {
  96.         String text = "";
  97.         for (int i = 0; i < a.length; i++) {
  98.             text += "[" + a[i] + "] ";
  99.         }
  100.         System.out.println(text);
  101.     }
  102.    
  103.     public static void main(String[] args) {
  104.  
  105.         int[] c = {3,6,2,7,1,8,2,9,3,7,4,3,8,4,1,7,3,7};
  106.         int[] f = split(c);
  107.         printArray(f);
  108.         System.out.println(merge.zaehler);
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement