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
| #include<iostream> #include<algorithm> #include<cstdlib> #include<ctime> using namespace std; const int maxn = 2e6 + 10; int a[maxn], n; typedef long long ll;
void merge(int a[], int low, int mid, int high) { int N = high - low + 1; int b[N]; int left = low, right = mid + 1, idx = 0; while (left <= mid && right <= high) b[idx++] = a[left] < a[right] ? a[left++] : a[right++]; while (left <= mid) b[idx++] = a[left++]; while (right <= high) b[idx++] = a[right++]; for (int i = 0; i < N; i++) a[low + i] = b[i]; }
void mergeSort(int a[], int low, int high) { if (low < high) { int mid = (low + high) / 2; mergeSort(a, low, mid); mergeSort(a, mid + 1, high); merge(a, low, mid, high); } } void print() { for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } int main() { ios::sync_with_stdio(false); cin >> n; srand(time(0)); for (int i = 0; i < n; i++) { a[i] = rand() % maxn; } print(); mergeSort(a, 0, n - 1); print(); return 0; }
|