Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. union Int32 {
  5. int x;
  6. unsigned char bytes[4];
  7. };
  8.  
  9. union Int32 *distributionsort(int cur_cur, int cur, int m, union Int32 *s, int n)
  10. {
  11. if (cur_cur == 0) {
  12. int count[m];
  13. int i, k, j = 0;
  14. for (i = 0; i < m; i++) count[i] = 0;
  15. while (j < n) {
  16. k = (s[j].bytes[cur]) % 10;
  17. count[k]++;
  18. j++;
  19. }
  20. i = 1;
  21. while (i < m) {
  22. count[i] = count[i] + count[i - 1];
  23. i++;
  24. }
  25. union Int32 *d = (union Int32 *) malloc(n * sizeof(union Int32));
  26. j = n - 1;
  27. while (j >= 0) {
  28. k = (s[j].bytes[cur]) % 10;
  29. i = count[k] - 1;
  30. count[k] = i;
  31. d[i].x = s[j].x;
  32. j--;
  33. }
  34. return d;
  35. }
  36. if (cur_cur == 1) {
  37. int count[m];
  38. int i, k, j = 0;
  39. for (i = 0; i < m; i++) count[i] = 0;
  40. while (j < n) {
  41. k = ((s[j].bytes[cur]) % 100) / 10;
  42. count[k]++;
  43. j++;
  44. }
  45. i = 1;
  46. while (i < m) {
  47. count[i] = count[i] + count[i - 1];
  48. i++;
  49. }
  50. union Int32 *d = (union Int32 *) malloc(n * sizeof(union Int32));
  51. j = n - 1;
  52. while (j >= 0) {
  53. k = ((s[j].bytes[cur]) % 100) / 10;
  54. i = count[k] - 1;
  55. count[k] = i;
  56. d[i].x = s[j].x;
  57. j--;
  58. }
  59. return d;
  60. }
  61. else {
  62. int count[m];
  63. int i, k, j = 0;
  64. for (i = 0; i < m; i++) count[i] = 0;
  65. while (j < n) {
  66. k = ((s[j].bytes[cur]) / 100);
  67. count[k]++;
  68. j++;
  69. }
  70. i = 1;
  71. while (i < m) {
  72. count[i] = count[i] + count[i - 1];
  73. i++;
  74. }
  75. union Int32 *d = (union Int32 *) malloc(n * sizeof(union Int32));
  76. j = n - 1;
  77. while (j >= 0) {
  78. k = ((s[j].bytes[cur]) / 100);
  79. i = count[k] - 1;
  80. count[k] = i;
  81. d[i].x = s[j].x;
  82. j--;
  83. }
  84. return d;
  85. }
  86. }
  87.  
  88. union Int32 *radixsort(union Int32 *s, int n)
  89. {
  90. union Int32 *d = (union Int32*)malloc(n * sizeof(union Int32));
  91. union Int32 *z;
  92. int i, j;
  93. for (i = 0; i < n; i++) d[i].x = s[i].x;
  94. for (i = 0; i < n; i++) {
  95. for (j = 0; j < 3; j++) {
  96. z = distributionsort(j, i, 10, d, n);
  97. free(d);
  98. d = z;
  99. }
  100. }
  101. return d;
  102. }
  103.  
  104. int main()
  105. {
  106. int i, n, cur, lenf = 0;
  107. scanf("%d\n", &n);
  108. union Int32 a[n];
  109. union Int32 f[n];
  110. for (i = 0; i < n; i++) {
  111. scanf("%d", &cur);
  112. if (cur < 0) {
  113. f[lenf].x = cur;
  114. lenf++;
  115. }
  116. else a[i - lenf].x = cur;
  117. }
  118. if (lenf > 0) {
  119. union Int32 *res = radixsort(f, lenf);
  120. for (i = 0; i < lenf; i++) {
  121. printf("%d ", res[i].x);
  122. }
  123. free(res);
  124. }
  125. union Int32 *res = radixsort(a, n - lenf);
  126. for (i = 0; i < n - lenf; i++) {
  127. if (i != n - 1) printf("%d ", res[i].x);
  128. else printf("%d\n", res[i].x);
  129. }
  130. free(res);
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement