Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void quicksort(int n, int m, int *a);
  4.  
  5. void quicksort_rec(int low, int high, int *a, int m);
  6.  
  7. int partition(int low, int high, int*a);
  8.  
  9. void selectsort(int low, int high, int *a);
  10.  
  11.  
  12.  
  13. int partition(int low, int high, int *a){
  14. int i=low, j=low, tmp;
  15. while (j<high){
  16. if (a[j]<a[high]){
  17. tmp=a[i];
  18. a[i]=a[j];
  19. a[j]=tmp;
  20. i+=1;
  21. }
  22. j+=1;
  23. }
  24. tmp=a[high];
  25. a[high]=a[i];
  26. a[i]=tmp;
  27. return i;
  28. }
  29.  
  30. void selectsort(int low, int high, int *a){
  31. int i, j=high, k, tmp;
  32. while (j>low){
  33. k=j;
  34. i=j-1;
  35. while (i>=low){
  36. if (a[i]>a[k]) k=i;
  37. i-=1;
  38. }
  39. tmp=a[j];
  40. a[j]=a[k];
  41. a[k]=tmp;
  42. j-=1;
  43. }
  44. }
  45.  
  46. void quicksort(int n, int m, int *a){
  47. quicksort_rec(0, n-1, a, m);
  48. }
  49.  
  50. void quicksort_rec(int low, int high, int *a, int m){
  51. if (low<high){
  52. if (high-low+1<m) selectsort(low, high, a);
  53. else{
  54. int q = partition(low, high, a);
  55. quicksort_rec(low, q-1, a, m);
  56. quicksort_rec(q+1, high, a, m);
  57. }
  58. }
  59. }
  60.  
  61. int main(int argc, char **argv){
  62. int n, m;
  63. scanf("%d%d", &n, &m);
  64. int a[n];
  65. for (int i=0; i<n; i++) scanf("%d", &a[i]);
  66. quicksort(n, m, a);
  67. for (int i=0; i<n; i++) printf("%d ", a[i]);
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement