// // Third homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Topic: MergeSort using recursion algorithm // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/18. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
template<classType> voidM_S(Typea[], intl, intr) { if (l==r) return; int mid = (l+r)/2; M_S(a,l,mid); M_S(a,mid+1,r); int b[10010]; for(int p1=l, p2 = mid+1,pj=l;pj<=r;pj++) { if (p1>mid) { b[pj] = a[p2++]; continue; } if (p2>r) { b[pj] = a[p1++]; continue; } if (a[p1]<=a[p2]) b[pj] = a[p1++]; else b[pj] = a[p2++]; } for (int i=l;i<=r;i++) a[i] = b[i]; return; } intmain() { int n; cin >> n; intlist[10010]; for (int i=0;i<n;i++) cin >> list[i]; M_S(list, 0, n-1); for (int i=0;i<n;i++) cout << list[i] << " "; cout << endl; }
// // Third homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Topic: MergeSort without recursion algorithm // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/18. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
template<classType> voidM_S(Typea[],intn) { int stp =1; while(1) { stp*=2; for (int i=0;i<n;i+=stp) { int l =i, r =min(i+stp-1,n-1); int mid = (l+r)/2; int b[10010]; for(int p1=l, p2 = mid+1,pj=l;pj<=r;pj++) { if (p1>mid) { b[pj] = a[p2++]; continue; } if (p2>r) { b[pj] = a[p1++]; continue; } if (a[p1]<=a[p2]) b[pj] = a[p1++]; else b[pj] = a[p2++]; } for (int j=l;j<=r;j++) a[j] = b[j]; } if (stp>n) break; } } intmain() { int n; cin >> n; intlist[10010]; for (int i=0;i<n;i++) cin >> list[i]; M_S(list,n); for (int i=0;i<n;i++) cout << list[i] << " "; cout << endl; }
// // Third homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Topic: MergeSort using its natural fragments // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/18. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
structnatual_per { int left; int right; }; natual_per se[10010]; template<classType> voidM_S(Typea[],intn) { int stp =1; while(1) { stp*=2; for (int i=1;i<=n;i+=stp) { int l =se[i].left; int r; if (i+stp-1>n) r = se[n].right; else r = se[i+stp-1].right; int mid = se[(i+min(i+stp-1,n))/2].right; //core part int b[10010]; for(int p1=l, p2 = se[min((i+min(i+stp-1,n))/2+1,n)].left,pj=l;pj<=r;pj++) //core part { if (p1>mid) { b[pj] = a[p2++]; continue; } if (p2>r) { b[pj] = a[p1++]; continue; } if (a[p1]<=a[p2]) b[pj] = a[p1++]; else b[pj] = a[p2++]; } for (int i=l;i<=r;i++) a[i] = b[i]; } if (stp>n) break; } } intmain() { int n; cin >> n; intlist[10010]; se[1].left = 0; cin >> list[0]; int tot=1; for (int i=1;i<n;i++) { cin >> list[i]; if (list[i]<list[i-1]) { se[tot].right = i-1; tot++; se[tot].left = i; } } se[tot].right = n-1; M_S(list,tot); for (int i=0;i<n;i++) cout << list[i] << " "; cout << endl; }
// // Third homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Topic: QuickSort using thr first element // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/18. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
template<classType> voidQS(Typea[],intleft, intright) { if (left>=right) return; Type stan = a[left]; int low = left, high = right; int pos; while (1) { for (int i=high;i>low;i--) { high = i; if (a[i]<stan) { a[low] = a[i]; low++; break; } if (low+1==high) high--; } if (low == high) { a[low] = stan; pos = low; break; } for (int i=low;i<high;i++) { low = i; if (a[i]>stan) { a[high] = a[i]; high--; break; } if (low+1==high) low++; } if (low == high) { a[low] = stan; pos = low; break; } } QS(a,left,pos-1); QS(a,pos+1,right); return; }
intmain() { int n; cin >> n; intlist[10010]; for (int i=0;i<n;i++) cin >> list[i]; QS(list, 0, n-1); for (int i=0;i<n;i++) cout << list[i] << " "; cout << endl; }