归并排序

归并排序

package com.buaa.algorithm.sort;

/** * 归并排序 * @author lzxyzq * */
public class d2mergesorted {
    static int[]merge(int[]a,int[]b){
        int[]x = new int[a.length+b.length];
        int ai=0;
        int bi=0;
        int xi=0;

        while(ai<a.length&&bi<b.length){
        if(a[ai]<=b[bi])
            x[xi++]=a[ai++];
        else
            x[xi++]=b[bi++];
        }
        while(ai<a.length) x[xi++]=a[ai++];
        while(bi<b.length) x[xi++]=b[bi++];
        return x;
    }

    public static void main(String[] args) {
        int[]a = {5,8,11,19,22,25};
        int[]b = {7,9,20,30,35,38,40};
        int[]c = merge(a,b);
        for(int i=0;i<c.length;i++)
            System.out.println(c[i]);
    }
}

你可能感兴趣的:(归并排序)