归并排序(模板)

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
int a[N];
int n;
int read()
{
    int f = 1 , s = 0;
    char c = getchar();
    while(c > '9' || c < '0') {if(c == '-') f = -1 ;c = getchar();}
    while(c >='0' && c <= '9') s = s * 10 + c - '0' , c = getchar();
    return s * f;
}
int h[N] ;
void merge_sort(int a[] , int l , int r)
{
    if(l >= r) return; 
    int mid = l + r >> 1 ;
    merge_sort(a , l , mid) , merge_sort(a , mid + 1 , r);
    int i = l , j  = mid + 1;
    int idx = 0;
    while(i <= mid && j <= r){
         
        if(a[i] <= a[j]) h[idx ++] = a[i ++];
        else h[idx ++] = a[j ++];
    }
    while(i <= mid) h[idx ++] = a[i ++];
    while(j <= r) h[idx ++] = a[j ++];
      
    for(int i = l , j = 0; i <= r ; j ++, i ++)
    {
        a[i] = h[j]; 
    }
    
}
int main()
{
    cin >> n;
    for(int i = 0 ; i < n ; i ++)
        a[i] = read();
    merge_sort(a , 0 , n - 1);
    // cout << a[0];
    for(int i = 0 ; i < n ; i ++ ) cout << a[i] <<" ";
}

上一篇:会议更流畅,表情更生动!视频生成编码 VS 国际最新 VVC 标准


下一篇:Cloud Toolkit 数据库 SQL 执行器