Vikhyath_11

p4b

Jul 27th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define MAXSIZE 30000
  6. #define NTIMES 5000
  7.  
  8. int partition(int a[], int low, int high) {
  9. int pivot = a[high];
  10. int i = low - 1;
  11. for (int j = low; j < high; j++) {
  12. if (a[j] < pivot) {
  13. i++;
  14. int temp = a[i];
  15. a[i] = a[j];
  16. a[j] = temp;
  17. }
  18. }
  19. int temp = a[i + 1];
  20. a[i + 1] = a[high];
  21. a[high] = temp;
  22. return i + 1;
  23. }
  24.  
  25. void quicksort(int a[], int low, int high) {
  26. if (low < high) {
  27. int pi = partition(a, low, high);
  28. quicksort(a, low, pi - 1);
  29. quicksort(a, pi + 1, high);
  30. }
  31. }
  32.  
  33. int main() {
  34. int a[MAXSIZE], n, k, i;
  35. double runtime = 0;
  36. clock_t start, end;
  37.  
  38. printf("Enter the value of n\n");
  39. scanf("%d", &n);
  40.  
  41. for (k = 1; k <= NTIMES; k++) {
  42. srand(time(0));
  43. for (i = 0; i < n; i++) {
  44. a[i] = rand();
  45. }
  46.  
  47. start = clock();
  48. quicksort(a, 0, n - 1);
  49. end = clock();
  50.  
  51. runtime += ((double)(end - start)) / CLOCKS_PER_SEC;
  52. }
  53.  
  54. runtime /= NTIMES;
  55.  
  56. printf("\nSorted elements are\n");
  57. for (i = 0; i < n; i++) {
  58. printf("%d\n", a[i]);
  59. }
  60.  
  61. printf("Time taken for sorting is %lf seconds\n", runtime);
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment