fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5.  
  6. void swap(int *x, int *y)
  7. {
  8. int temp = *x;
  9. *x = *y;
  10. *y = temp;
  11. return ;
  12. }
  13.  
  14. int median3(int a[], int left, int right)//Uses median of three partitioning technique
  15. {
  16. int center = (left + right)/2;
  17. if (a[center] < a[left])
  18. swap(&a[left],&a[center]);
  19. if (a[right] < a[left])
  20. swap(&a[left],&a[right]);
  21. if (a[right]< a[center])
  22. swap(&a[center],&a[right]);
  23.  
  24. swap(&a[center], &a[right - 1]);//since the largest is already in the right.
  25. return a[right - 1];
  26. }
  27.  
  28. void quicksort(int a[], int left, int right)
  29. {
  30. if (left < right) {
  31. int pivot = median3(a,left,right);
  32. if (left == right - 1) return;
  33. int i = left;
  34. int j = right - 1;
  35. for ( ; ;) {
  36. while(a[++i]<pivot) {}
  37. while(pivot<a[--j]) {}
  38. if ( i < j)
  39. swap(&a[i],&a[j]);
  40. else
  41. break ;
  42. }
  43. swap(&a[i],& a[right -1]);
  44. quicksort(a,left,i-1);
  45. quicksort(a,i+1,right);
  46. }
  47. return ;
  48. }
  49.  
  50. int main(int argc, char* argv[])
  51. {
  52. int i;
  53. int a[] = {8,1,4,9,6,3,5,2,7,0};
  54. quicksort(a,0,9);
  55. for ( i = 0 ; i < 10 ; i++)
  56. printf("%d\n",a[i]);
  57. return 0;
  58. }
  59.  
  60.  
Success #stdin #stdout 0.02s 1720KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6
7
8
9