BottomupSort算法 c++代码实现
生活随笔
收集整理的這篇文章主要介紹了
BottomupSort算法 c++代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
using namespace std;#define N 100int 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;
}
?
總結
以上是生活随笔為你收集整理的BottomupSort算法 c++代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Solr 部分 局部字段修改 更新 删除
- 下一篇: 【C++探索之旅】第一部分第四课:内存,