fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void p(int const *b, int const *e) {
  5. while (b < e) printf("%d ", *b++);
  6. puts("");
  7. }
  8. int *idup(int const *b, int const *e) {
  9. int *p = malloc((e - b) * sizeof *b);
  10. if (p) memcpy(p, b, (e - b) * sizeof *b);
  11. return p;
  12. }
  13. int icmp(void const *a, void const *b) {
  14. return *(int *)a - *(int *)b;
  15. }
  16. void f(int *b, int *e) {
  17. int size = e - b, *buff = idup(b, e), *p, *q;
  18. qsort(buff, size, sizeof *b, icmp);
  19. #define odd(n) ((n) % 2)
  20. #define even(n) (!odd(n))
  21. for (p = buff, q = p + size - 1; b < e; b++)
  22. if (odd(*b)) {while (even(*p)) p++; *b = *p++;}
  23. else {while (odd(*q)) q--; *b = *q-- / 2;}
  24. free(buff);
  25. }
  26. void g(int *b, int *e) {
  27. p(b, e), f(b, e), p(b, e);
  28. }
  29. int main() {
  30. int a[] = {0, 9, 8, 4, 6, 5, 1, 2, 7, 3};
  31. #define size(a) (sizeof a / sizeof *a)
  32. g(a, a + size(a));
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 4248KB
stdin
Standard input is empty
stdout
0 9 8 4 6 5 1 2 7 3 
4 1 3 2 1 3 5 0 7 9