Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- union Int32 {
- int x;
- unsigned char bytes[4];
- };
- union Int32 *distributionsort(int cur_cur, int cur, int m, union Int32 *s, int n)
- {
- if (cur_cur == 0) {
- int count[m];
- int i, k, j = 0;
- for (i = 0; i < m; i++) count[i] = 0;
- while (j < n) {
- k = (s[j].bytes[cur]) % 10;
- count[k]++;
- j++;
- }
- i = 1;
- while (i < m) {
- count[i] = count[i] + count[i - 1];
- i++;
- }
- union Int32 *d = (union Int32 *) malloc(n * sizeof(union Int32));
- j = n - 1;
- while (j >= 0) {
- k = (s[j].bytes[cur]) % 10;
- i = count[k] - 1;
- count[k] = i;
- d[i].x = s[j].x;
- j--;
- }
- return d;
- }
- if (cur_cur == 1) {
- int count[m];
- int i, k, j = 0;
- for (i = 0; i < m; i++) count[i] = 0;
- while (j < n) {
- k = ((s[j].bytes[cur]) % 100) / 10;
- count[k]++;
- j++;
- }
- i = 1;
- while (i < m) {
- count[i] = count[i] + count[i - 1];
- i++;
- }
- union Int32 *d = (union Int32 *) malloc(n * sizeof(union Int32));
- j = n - 1;
- while (j >= 0) {
- k = ((s[j].bytes[cur]) % 100) / 10;
- i = count[k] - 1;
- count[k] = i;
- d[i].x = s[j].x;
- j--;
- }
- return d;
- }
- else {
- int count[m];
- int i, k, j = 0;
- for (i = 0; i < m; i++) count[i] = 0;
- while (j < n) {
- k = ((s[j].bytes[cur]) / 100);
- count[k]++;
- j++;
- }
- i = 1;
- while (i < m) {
- count[i] = count[i] + count[i - 1];
- i++;
- }
- union Int32 *d = (union Int32 *) malloc(n * sizeof(union Int32));
- j = n - 1;
- while (j >= 0) {
- k = ((s[j].bytes[cur]) / 100);
- i = count[k] - 1;
- count[k] = i;
- d[i].x = s[j].x;
- j--;
- }
- return d;
- }
- }
- union Int32 *radixsort(union Int32 *s, int n)
- {
- union Int32 *d = (union Int32*)malloc(n * sizeof(union Int32));
- union Int32 *z;
- int i, j;
- for (i = 0; i < n; i++) d[i].x = s[i].x;
- for (i = 0; i < n; i++) {
- for (j = 0; j < 3; j++) {
- z = distributionsort(j, i, 10, d, n);
- free(d);
- d = z;
- }
- }
- return d;
- }
- int main()
- {
- int i, n, cur, lenf = 0;
- scanf("%d\n", &n);
- union Int32 a[n];
- union Int32 f[n];
- for (i = 0; i < n; i++) {
- scanf("%d", &cur);
- if (cur < 0) {
- f[lenf].x = cur;
- lenf++;
- }
- else a[i - lenf].x = cur;
- }
- if (lenf > 0) {
- union Int32 *res = radixsort(f, lenf);
- for (i = 0; i < lenf; i++) {
- printf("%d ", res[i].x);
- }
- free(res);
- }
- union Int32 *res = radixsort(a, n - lenf);
- for (i = 0; i < n - lenf; i++) {
- if (i != n - 1) printf("%d ", res[i].x);
- else printf("%d\n", res[i].x);
- }
- free(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement