Actual MergeSort (W04)

    0

    0

    anlinn

      public int[] merge(int[] arr1, int[] arr2) {
      int i = 0;
      int j = 0;
      int[] res = new int[arr1.length + arr2.length];
      for (int x = 0; x < res.length; x++) {
      // Check if i and j are out of bound of array
      if (i == arr1.length) {
        res[x] = arr2[j++];
      } else if (j == arr2.length) {
        res[x] = arr1[i++];
      }
      // Compare arrays at the same point
      else if (arr1[i] < arr2[j]) {
        res[x] = arr1[i++];
      } else {
        res[x] = arr2[j++];
      }
      System.out.println("i ist:"+i+" j ist:"+j+" Sortiert:"+ Arrays.toString(res));
      }
      return res;
    
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.