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];

    }

  • 相关阅读:
    UIStepper
    UIProgressView
    Python——GUI编程 利息计算器 作业9(python programming)
    Python——GUI编程(python programming)
    麻烦的聚餐(最长递增子序列+二分优化)
    奶牛渡河(dp)
    POJ 1182-食物链 [并查集] 《挑战程序设计竞赛》2.4
    [2013百度软件研发笔试题] 求字符串中连续出现同样字符的最大值
    Java OCR tesseract 图像智能字符识别技术
    ORA-00904:&quot;T1&quot;.&quot;AREA_ID&quot; :标识符无效
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290367.html
Copyright © 2011-2022 走看看