Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void sherry()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- void merg(int arr[], int l, int m, int r)
- {
- int i, j, k = l;
- int n1 = m - l + 1, n2 = r - m;
- int L[n1], R[n2];
- for (int i = 0; i < n1; i++)
- L[i] = arr[l + i];
- for (int i = 0; i < n2; i++)
- R[i] = arr[m + i + 1];
- i = j = 0;
- while (i < n1 and j < n2)
- {
- if (L[i] < R[j])
- {
- arr[k] = L[i];
- i++;
- }
- else
- {
- arr[k] = R[j];
- j++;
- }
- k++;
- }
- while (i < n1)
- {
- arr[k] = L[i];
- i++, k++;
- }
- while (j < n2)
- {
- arr[k] = R[j];
- j++, k++;
- }
- }
- void mergeSort(int arr[], int l, int r)
- {
- if (l < r)
- {
- int m = (l + r) / 2;
- mergeSort(arr, l, m);
- mergeSort(arr, m + 1, r);
- merg(arr, l, m, r);
- }
- }
- int main()
- {
- sherry();
- ll n;
- cin >> n;
- int arr[n];
- for (int i = 0; i < n; i++)
- {
- cin >> arr[i];
- }
- mergeSort(arr, 0, n - 1);
- for (int i = 0; i < n; i++)
- cout << arr[i] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement