Vikhyath_11

p4man

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