Advertisement
Guest User

Untitled

a guest
May 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void selectsort (long *a, long c, long d){
  5. long j, i, k, t;
  6. for (j=d; j>c; j--){
  7. for (i=j-1, k=j; i>=c; i--){
  8. if (a[i]>a[k]) k=i;
  9. }
  10. t = a[j];
  11. a[j] = a[k];
  12. a[k] = t;
  13. }
  14. }
  15.  
  16.  
  17. long partition (long *a, long c, long d){
  18.  
  19. long i=c, j=d, ser, max, min,t, q=0;
  20.  
  21. ser=(d-c-1)/2;
  22. if (ser>0){
  23. for (i=c, max=c; i<=d; i+=ser){
  24. if (a[i]>a[max]) max=i;
  25. }
  26. for (i=c, min=c; i<=d; i+=ser){
  27. if (a[i]<a[min]) min=i;
  28. }
  29. for (i=c; i<=d; i+=ser){
  30. if ((i!=max) && (i!=min)) q=i;
  31. }
  32. }
  33. else q=d;
  34. printf("%ld ", q);
  35.  
  36.  
  37. for (i=c, j=c; j<=d; j++){
  38. if (a[j]<a[q]){
  39. t = a[i];
  40. a[i] = a[j];
  41. a[j] = t;
  42. i++;
  43. }
  44. }
  45.  
  46. t = a[i];
  47. a[i] = a[q];
  48. a[q] = t;
  49.  
  50. return(i);
  51.  
  52. }
  53.  
  54. void quicksortrec(long* a, long nel, long c, long d, long m){
  55.  
  56. long q;
  57.  
  58. if ((c-d+1)<m) selectsort(a, c, d);
  59. else {
  60. if (c<d){
  61. q=partition(a, c, d);
  62. quicksortrec(a, nel, c, q-1, m);
  63. quicksortrec(a, nel, q+1, d, m);
  64. }
  65.  
  66. }
  67.  
  68. }
  69.  
  70. void quicksort(long *a, long nel, long m){
  71. quicksortrec(a, nel, 0, nel-1, m);
  72. }
  73.  
  74. int main(){
  75.  
  76. long i, n, m;
  77. long *a;
  78. scanf("%ld", &n);
  79. scanf("%ld", &m);
  80.  
  81. a = (long*)malloc(n * sizeof(long));
  82. for (i = 0; i < n; i++) scanf("%ld", a+i);
  83.  
  84. quicksort(a, n, m);
  85. for (i = 0; i < n; i++) printf("%ld ", a[i]);
  86. free(a);
  87.  
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement