zoukankan      html  css  js  c++  java
  • BottomupSort算法 c++代码实现

    #include <iostream>
    using namespace std;
    
    #define N 100
    
    int A[N];     
    static int n;   
    
    
    void Initial()
    {
        cout << "请输入元素的个数:";
        cin >> n;
        cout << "请输入" << n << "个元素:";
        for(int i = 1; i <=n; i ++)
        {
            cin >> A[i];
        }
    }
    
    void Print()
    {
        cout << "经过Bottomupsort后:";
        for(int i = 1; i <=n; i ++)
        {
            cout << A[i] << " ";
        }
        cout << endl;
    }
    
    
    void Merge(int a[], int p, int q, int r)
    {
        int b[N];
        int s = p, t = q+1, k = p;
        
        while(s <= q && t <= r)
        {
            if(a[s] <= a[t])
            {
                b[k++] = a[s++];
            }
            else
            {
                b[k++] = a[t++];
            }
        }
    
        if(s==q+1)
        {
            for(int i = t; i <= r; i ++)
            {
                b[k++] = a[i];
            }
        }
        else
        {
            for(int j = s; j <= q; j ++)
            {
                b[k++] = a[j];
            }
        }
    
        //把b[]中排好的元素copy到a[]中
        for(int i = p; i <= r; i++)
        {
            a[i] = b[i];
        }
    }
    
    
    void Bottomupsort(int a[],int n)
    {
    
       int t = 1;
       int s,i;
       while (t<n)
       {
            s = t;t =2*s;i=0;
            while(i+t<=n)
            {
                Merge(a,i+1,i+s,i+t);
                i = i+t;
            }
            if(i+s<n)
            {
                Merge(a,i+1,i+s,n);
            }
       }
    }
    
    
    int main()
    {
        Initial();
    
        if(n > 1)
        {
            Bottomupsort(A,n);
            Print();
        }
        else if(n == 1)
        {
            Print();
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    stat 命令家族(2)- 详解 pidstat
    stat 命令家族(1)- 详解 vmstat
    Linux常用命令
    Linux常用命令
    Linux常用命令
    Linux常用命令
    Linux常用命令
    Linux常用命令
    打通MySQL架构和业务的任督二脉
    PostgreSQL JOIN LATERAL
  • 原文地址:https://www.cnblogs.com/loveincode/p/4496639.html
Copyright © 2011-2022 走看看