package com.test.sort; public class testMerge { public static void main(String[] args) { int[] a = { 1, 3, 5 }; int[] b = { 2, 3, 4, 7 }; merge m = new merge(); m.method(a, b); } } class merge { public void method(int[] a, int[] b) { int l = a.length + b.length; int[] temp = new int[l]; int i = 0, j = 0, h = 0; // 这里必须用while,不能用if while (i < a.length || j < b.length) { if (i == a.length && j < b.length) { temp[h++] = b[j++]; } else if (i < a.length && j == b.length) { temp[h++] = a[i++]; } else if (a[i] <= b[j]) { temp[h++] = a[i++]; } else if (a[i] > b[j]) { temp[h++] = b[j++]; } } for (int m : temp) { System.out.print(m + " "); } } }
自己业余做的公众号和小程序,欢迎各位朋友扫码关注,发现更多精彩内容
转载请注明原文地址: https://ju.6miu.com/read-675372.html