Vikhyath_11

p3

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