zoukankan      html  css  js  c++  java
  • 55.插入排序与归并排序

    插入排序

    代码:

    #include

    using namespace std;

    int main()

    {

           int a[101];

           int n;

           cin>>n;

           for(int i=1;i<=n;++i)

           cin>>a[i];

           for(int i=2;i<=n;++i)

           {

                  int x=a[i];

                  int j=i-1;

                  while(x=1)

                  {

                         a[j+1]=a[j];

                         j--;

                  }

                  a[j+1]=x;

           }

           for(int i=1;i<=n;++i)

           cout<<a[i]<<" ";

           return 0;

    }

    归并排序

    代码:

    #include

    using namespace std;

    #include

    void gb(int,int);

    int a[101],r[101];

    int main()

    {

           int n;

           cin>>n;

           for(int i=1;i<=n;++i)

           cin>>a[i];

           gb(1,n);

           for(int i=1;i<=n;++i)

           cout<<a[i]<<" ";

           return 0;

    }

    void gb(int s,int t)

    {

           if(s==t) return;

           int m=(s+t)/2;

           gb(s,m);gb(m+1,t);

           int i=s,k=s,j=m+1;

           while(i<=m&&j<=t)

           {

                  if(a[i]

                  {

                         r[k]=a[i];

                         i++;

                         k++;

                  }

                  else {

                         r[k]=a[j];

                         j++;

                         k++;

                  }

          

           }

                  while(i<=m)

                  {

                         r[k]=a[i];

                         i++;k++;

                  }

                  while(j<=t)

                  {

                         r[k]=a[j];

                         j++;k++;

                  }

                  for(int i=s;i<=t;++i)

                  a[i]=r[i];

    }

  • 相关阅读:
    2020十一届蓝桥杯-七月省赛题解
    ES6 Generator与C#迭代器
    .NET Core微服务部分知识点
    js点击图片传给一个空的div内放大显示
    Jmeter 中 CSV 如何参数化测试数据并实现自动断言
    安装nvidia-docker2报错E: Unable to locate package nvidia-docker2
    微信小程序-点击事件传递参数
    2020牛客暑期多校训练营(第八场)I Interesting Computer Game K Kabaleo Lite
    Count the Colors
    Basic Gcd Problem
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290366.html
Copyright © 2011-2022 走看看