• Bio

    Just do it! 勇敢去做

    归并排序模版

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e6+10;
    int n;
    int a[N],temp[N];   //temp[]为辅助空间
    void merge_sort(int a[],int l,int r){
        //1个或者0个
        if(l==r) return;
    
        //(1)确定中间点
        int mid=l+r>>1;
        //(2)递归排序
        merge_sort(a,l,mid);
        merge_sort(a,mid+1,r);
    
        //(3)归并
        //k表示当前合并了几个数了,也就是temp里面有几个数了;i,j是两指针起点
        int k=0,i=l,j=mid+1;
        while(i<=mid && j<=r){
            if(a[i]<a[j]) temp[k++]=a[i++];
            else temp[k++]=a[j++];
        }
        //左边没有循环完
        while(i<=mid) temp[k++]=a[i++];
        //右边没有循环完
        while(j<=r) temp[k++]=a[j++];
     
        for(i=l,j=0;i<=r;i++,j++){
            a[i]=temp[j];
        }
    }
    int main(){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        merge_sort(a,0,n-1);
        for(int i=0;i<n;i++) printf("%d ",a[i]);
        return 0;
    }
    
    

    124.222.182.130

  • Accepted Problems

  • Recent Activities

    This person is lazy and didn't join any contests or homework.
  • Recent Solutions

    This person is lazy and didn't write any solutions.
  • Stat

  • Rating

Problem Tags

2016
3
NOIP 普及组
3
USACO
2
状态压缩
2
2008
1
2015
1
2019
1
搜索
1
深度优先搜索
1
DFS
1
Floyd
1
拓扑排序
1
排序
1
树形 dp
1
动态规划
1
dp
1
状压
1
COCI
1
动态规划,dp
1
蓝桥杯省赛
1