Guest User

Untitled

a guest
Nov 15th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. int n;
  2. int *a;
  3.  
  4. void enter()
  5. {
  6. srand(time(0));
  7. cin>>n;
  8. a=new int[n];
  9. for(int i=0;i<n-1;i++) a[i]=rand()%100;
  10. }
  11.  
  12. template <typename T>
  13. void mergeSort(T *pD,int N)
  14. {
  15. if (N<2) return;
  16. mergeSort(pD,N/2);
  17. mergeSort(pD+N/2,N-N/2);
  18. T *pT=new T[N/2];
  19. memcpy(pT,pD,sizeof(T)*(N/2));
  20. int p1Idx,p2Idx,pI;
  21. p1Idx=pI=0;
  22. p2Idx=N/2;
  23. while (1)
  24. {
  25. if (pT[p1Idx]<pD[p2Idx]) pD[pI++]=pT[p1Idx++];
  26. else pD[pI++]=pD[p2Idx++];
  27. if (p1Idx>=N/2||p2Idx>=N) break;
  28. }
  29. while (p1Idx<N/2) pD[pI++]=pT[p1Idx++];
  30. }
  31.  
  32. template <typename T>
  33. void shellSort(T *a,int n)
  34. {
  35. int j,temp;
  36. for(int gap=n/2;gap>0;gap/=2)
  37. {
  38. for(int i=gap;i<n;i++)
  39. {
  40. j=i;
  41. temp=a[i];
  42. for(j=i;j>=gap&&a[j-gap]>temp;j-=gap) a[j]=a[j-gap];
  43. a[j]=temp;
  44. }
  45. }
  46. }
  47.  
  48. void solveMerge()
  49. {
  50. clock_t begin,end;
  51. begin=clock();
  52. mergeSort(a,n);
  53. end=clock();
  54. cout<<"Merge sort time: "<<(double(end-begin))/1000.0<<'n';
  55. }
  56.  
  57. void solveShell()
  58. {
  59. clock_t begin,end;
  60. begin=clock();
  61. shellSort(a,n);
  62. end=clock();
  63. cout<<"Shell sort time: "<<(double(end-begin))/1000.0<<'n';
  64. }
  65.  
  66. int main()
  67. {
  68. enter();
  69. solveMerge();
  70. solveShell();
  71. }
Add Comment
Please, Sign In to add comment