Vikhyath_11

p4

Jul 26th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #define max 30000
  5. #define ntimes 2000
  6. void swap(int *a,int *b){
  7. int temp=*a;
  8. *a=*b;
  9. *b=temp;
  10. }
  11. int partition(int a[],int lb,int ub){
  12. int pivot=a[lb];
  13. int start=lb;
  14. int end=ub;
  15. while(start<end){
  16. while(a[start]<=pivot){
  17. start++;
  18. }
  19. while(a[end]>pivot){
  20. end--;
  21. }
  22. if(start<end){
  23. swap(&a[start],&a[end]);
  24. }
  25. }
  26. swap(&a[lb],&a[end]);
  27. return end;
  28. }
  29. void quicksort(int a[],int lb,int ub){
  30. if(lb<ub){
  31. int loc=partition(a,lb,ub);
  32. quicksort(a,lb,loc-1);
  33. quicksort(a,loc+1,ub);
  34. }
  35. }
  36. int main(){
  37. int a[max],k,i,n;
  38. double runtime=0;
  39. clock_t start,end;
  40. printf("enter the num of values");
  41. scanf("%d",&n);
  42. srand(time(NULL));
  43. for(i=0;i<n;i++){
  44. a[i]=rand();
  45. }
  46. start=clock();
  47. quicksort(a,0,n-1);
  48. end=clock();
  49. runtime=(double)(end-start)/CLOCKS_PER_SEC;
  50. printf("the elements are:");
  51. for(i=0;i<n;i++)
  52. {
  53. printf("%d\n",&a[i]);
  54. }
  55. printf("the runtime=%lf",runtime);
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment