Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include<cmath>
  3. #include<climits>
  4. #include<algorithm>
  5. using namespace std;
  6. void merge_series(double* A, int b, int c, int e, double* D)
  7. {
  8. int i = b, j = c + 1, k;
  9. for (k = b; k <= e; k++)
  10. if (j > e) D[k] = A[i++];
  11. else if (i > c) D[k] = A[j++];
  12. else if (A[i] <= A[j]) D[k] = A[i++];
  13. else D[k] = A[j++];
  14. }
  15. void merge_sort(double* A, int n)
  16. {
  17. int s, b, c, e;
  18. double* D = new double[n];
  19. for (s = 1; s < n; s *= 2) {
  20. for (b = 0; b < n; b += s * 2) {
  21. c = min(b + s - 1, n - 1);
  22. e = min(c + s, n - 1);
  23. merge_series(A, b, c, e, D);
  24. }
  25. for (b = 0; b < n; b++) A[b] = D[b];
  26. }
  27. for (b = 0; b < n; b++) cout << A[b] << " ";
  28. delete[] D;
  29. }
  30.  
  31. int main()
  32. {
  33. freopen("input.txt", "r", stdin);
  34. freopen("output.txt", "w", stdout);
  35. int n;
  36. cin >> n;
  37. double* A = new double[n];
  38. for (int i = 0; i < n; i++)
  39. {
  40. cin >> A[i];
  41. }
  42. merge_sort(A, n);
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement