Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int v[100001];
- void interclasare (int v[], int st, int dr, int mid)
- {
- int i, j, k;
- int nst = mid - st + 1;
- int ndr = dr - mid;
- int vst[nst], vdr[ndr];
- for (i = 0; i < nst; i++)
- vst[i] = v[st + i];
- for (j = 0; j < ndr; j++)
- vdr[j] = v[mid + j + 1];
- i = 0;
- j = 0;
- k = st;
- while (i < nst && j < ndr)
- {
- if (vst[i] <= vdr[j])
- {
- v[k] = vst[i];
- i++;
- }
- else
- {
- v[k] = vdr[j];
- j++;
- }
- k++;
- }
- while (i < nst)
- {
- v[k] = vst[i];
- i++;
- k++;
- }
- while (j < ndr)
- {
- v[k] = vdr[j];
- j++;
- k++;
- }
- }
- void sortare(int v[], int st, int dr)
- {
- int mid;
- if (st < dr)
- {
- mid = (st + dr)/2;
- sortare(v, st, mid);
- sortare(v, mid + 1, dr);
- interclasare(v, st, dr, mid);
- }
- }
- int main()
- {
- int n;
- cin >> n;
- for (int i = 0; i < n; i++)
- cin >> v[i];
- sortare(v, 0, n - 1);
- for (int i = 0; i < n; i++)
- cout << v[i] << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement