• 归并排序



    #include<iostream>
    #include<assert.h>
    using namespace std;


    int temp[20];

    void domerge(int*a, int left, int right){

    for (int i = left; i <= right; i++)
    temp[i] = a[i];


    int low = left;
    int mid = (left + right) / 2;
    int high = (left + right) / 2 + 1;

    for (int i = left; i <= right; i++){

    if (low>mid){
    a[i] = temp[high++];
    }
    else if (high>right){
    a[i] = temp[low++];
    }
    else if (temp[low]<temp[high]){
    a[i] = temp[low++];
    }
    else {
    a[i] = temp[high++];
    }


    }


    }
    void merge(int*a, int left, int right){
    if (left >= right) return;
    int mid = (left + right) / 2;

    merge(a, left, mid);
    merge(a, mid + 1, right);
    domerge(a, left, right);

    }


    int main(){
    int temp[12] = { 81, 4, 6, 2, 33, 6, 1, 6, 3, 4, 3, 0 };
    merge(temp, 0, 11);

    for (int i = 0; i < 12; i++)
    cout << temp[i] << " ";
    getchar();
    return 0;
    }

  • 相关阅读:
    CF 436D 最小生成树
    HDU 1847 博弈
    ZOJ 3666 博弈 SG函数
    zoj3675 BFS+状态压缩
    HDU 4734 F(x) 数位DP
    HDU 3709 Balanced Number 数位DP
    HDU 3555 数位DP
    HDU 4336 Card Collector
    HDU4340 Capturing a country DP
    CF 351A
  • 原文地址:https://www.cnblogs.com/liangyc/p/11646064.html
Copyright © 2020-2023  润新知