首页 > 编程语言 > 详细

归并排序模板(Java)

时间:2020-03-07 09:28:12      阅读:64      评论:0      收藏:0      [点我收藏+]
import java.util.Scanner;
import java.io.BufferedInputStream;
class Main{
    static int N = (int) 1e5 + 10,len;
    static int[] q = new int[N],temp = new int[N];
    static void mergeSort(int[] q,int l,int r){
        if(l>=r) return;
        int mid=(l+r)>>1;
        mergeSort(q,l,mid);
        mergeSort(q,mid+1,r);
        int k=0,i=l,j=mid+1;
        while(i<=mid&&j<=r)
            if(q[i]<q[j])temp[k++]=q[i++];
            else temp[k++]=q[j++];
        while(i<=mid) temp[k++]=q[i++];
        while(j<=r)temp[k++]=q[j++];
        for(k=0,i=l;i<=r;++i,++k)
            q[i]=temp[k];
    }

    public static void main(String[] args){
        Scanner sc=new Scanner(new BufferedInputStream(System.in));
        len=sc.nextInt();
        for(int x=0;x<len;++x)
            q[x]=sc.nextInt();
        mergeSort(q,0,len-1);
        for(int x:q)
            System.out.print(x+" ");
    }
}

归并排序模板(Java)

原文:https://www.cnblogs.com/INnoVationv2/p/12432305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!