fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define FIN "algsort.in"
  4. #define FOUT "algsort.out"
  5.  
  6. struct Arr {
  7.  
  8. int size;
  9. int *data;
  10. };
  11.  
  12. typedef struct Arr Arr;
  13.  
  14. Arr* Array(int size) {
  15.  
  16. Arr* v = (Arr*)malloc(sizeof(Arr) * size);
  17.  
  18. v->data = (int*)malloc(sizeof(int) * size);
  19.  
  20. v->size = size;
  21.  
  22. for (int i = 0; i < size; ++i)
  23. {
  24. v->data[i] = 0;
  25. }
  26.  
  27. return v;
  28. }
  29.  
  30. void run_combo(Arr *v) {
  31.  
  32. float shrinkFactor = 1.3;
  33. int gap = v->size,
  34. count = v->size,
  35. swapped = 0,
  36. i;
  37.  
  38. while(gap > 1 || swapped) {
  39.  
  40. swapped = 0;
  41.  
  42. if(gap > 1) gap = gap/shrinkFactor;
  43.  
  44. for (int i = 0; (i + gap) < count; ++i)
  45. {
  46. if(v->data[i] > v->data[i+gap]) {
  47.  
  48. int holder = v->data[i];
  49.  
  50. v->data[i] = v->data[i+gap];
  51.  
  52. v->data[i+gap] = holder;
  53.  
  54. swapped = 1;
  55. }
  56. }
  57. }
  58.  
  59. }
  60.  
  61. void display(Arr* arr) {
  62.  
  63. //freopen(FOUT, "w", stdout);
  64.  
  65. int count = arr->size;
  66.  
  67. for (int i = 0; i < count; ++i)
  68. {
  69. printf("%d ", arr->data[ i ]);
  70. }
  71. }
  72.  
  73.  
  74. int main(int argc, char const *argv[])
  75. {
  76. int n;
  77.  
  78. //freopen(FIN, "r", stdin);
  79.  
  80. scanf("%d", &n);
  81.  
  82. Arr* v = Array( n );
  83.  
  84. for (int i = 0; i < n; ++i)
  85. {
  86. scanf("%d", &v->data[i]);
  87. }
  88.  
  89. run_combo(v);
  90.  
  91. display(v);
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0s 4280KB
stdin
11
9 8 -7 6 5 4 3 2 1 0 -1
stdout
-7 -1 0 1 2 3 4 5 6 8 9