1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <iostream> #include <vector> using namespace std;
#define INFTY 100000000
void merge(vector<int>& A, int i, int f){ int mid = (i + f)/2; vector<int> R, L; for (int j = i; j <= mid; j++){ L.push_back(A[j]); } for (int j = mid + 1; j <= f; j++){ R.push_back(A[j]); } L.push_back(INFTY); R.push_back(INFTY); int a = 0, b = 0; for (int j = i; j <= f; ++j){ if(L[a] <= R[b]){ A[j] = L[a]; a++; }else{ A[j] = R[b]; b++; } } }
void mergesort(vector<int>& A, int i, int f){ if(i < f){ int mid = (i + f)/2; mergesort(A, i, mid); mergesort(A, mid+1, f); merge(A, i, f); } }
int main(){ vector<int> A; int n; scanf("%d", &n); for (int i = 0; i < n; ++i){ int tmp; scanf("%d", &tmp); A.push_back(tmp); } mergesort(A, 0, n - 1); for (int i = 0; i < n; ++i){ printf("%d ", A[i]); } return 0; }
|
This is copyright.