Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int n;
- vector<int> unsort(n);
- void merge(int l, int mid, int r)
- {
- int ptl = 0, ptr = mid - l + 1;
- vector<int> unstd(0);
- for (int i = l; i <= r; i++)
- {
- unstd.push_back(unsort[i]);
- }
- for (int i = l; i <= r; i++)
- {
- if (unstd[ptl] <= unstd[ptr])
- {
- unsort[i] = unstd[ptl];
- if (ptl == mid - l)
- {
- unstd[ptl] = 1000000000;
- continue;
- }
- ptl++;
- }
- else
- {
- unsort[i] = unstd[ptr];
- if (ptr == r - l)
- {
- unstd[ptr] = 1000000000;
- continue;
- }
- ptr++;
- }
- }
- }
- void sort(int l, int r)
- {
- if (l < r)
- {
- int mid = (l + r) / 2;
- sort(l, mid);
- sort(mid + 1, r);
- merge(l, mid, r);
- }
- }
- int main()
- {
- cin >> n;
- unsort = vector<int>(n);
- for (int i = 0; i < n; i++)
- {
- cin >> unsort[i];
- }
- sort(0, unsort.size() - 1);
- cout << endl;
- for (int i = 0; i < unsort.size(); i++)
- {
- cout << unsort[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement