fork download
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. void swap(int* a, int* b)
  5. {
  6. int t = *a;
  7. *a = *b;
  8. *b = t;
  9. }
  10.  
  11. int partition(int a[], int l, int h)
  12. {
  13. int x = a[h];
  14. int i = (l - 1);
  15.  
  16. for (int j = l; j <= h - 1; j++) {
  17. if (a[j] <= x) {
  18. i++;
  19. swap(&a[i], &a[j]);
  20. }
  21. }
  22. swap(&a[i + 1], &a[h]);
  23. return (i + 1);
  24. }
  25.  
  26. void quickSort(int a[], int l, int h)
  27. {
  28. int stack[h - l + 1];
  29.  
  30. int top = -1;
  31.  
  32. stack[++top] = l;
  33. stack[++top] = h;
  34.  
  35. while (top >= 0) {
  36. h = stack[top--];
  37. l = stack[top--];
  38.  
  39. int p = partition(a, l, h);
  40.  
  41. if (p - 1 > l) {
  42. stack[++top] = l;
  43. stack[++top] = p - 1;
  44. }
  45.  
  46. if (p + 1 < h) {
  47. stack[++top] = p + 1;
  48. stack[++top] = h;
  49. }
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. clock_t start, end;
  56. double cpu_time_used;
  57.  
  58. start = clock();
  59.  
  60. int n,i,a[100000];
  61. scanf("%d",&n);
  62. for(i=0;i<n;i++)
  63. scanf("%d",&a[i]);
  64. quickSort(a, 0, n - 1);
  65. for (i = 0; i < n; ++i)
  66. printf("%d ", a[i]);
  67. end = clock();
  68. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  69. printf("The time of execution is: %lf", cpu_time_used);
  70. return 0;
  71. }
Success #stdin #stdout 0s 4304KB
stdin
5
6
4
3
5
7
stdout
3  4  5  6  7  The time of execution is: 0.000045