Advertisement
Guest User

Interclasarea a doi vectori / MERGESORT

a guest
Feb 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include<iostream.h>
  2.  
  3. int x[50], n, i;
  4. void divizeaza(int s, int d, int&m)
  5. {
  6. m=(s+d)/2;
  7. }
  8. void interclaseaza(int s, int d, int m)
  9. {
  10. int i=s,j=m+1,k=1,v[100];
  11.  
  12. while(i<=m && j<=d)
  13. {
  14. if(x[i]<x[j]) {v[k]=x[i];i++;}
  15. else  {v[k]=x[j];j++;}
  16. k++;
  17. }
  18. if(i<=m) while(i<=m) { v[k]=x[i]; i++; k++; }
  19. else     while(j<=d) { v[k]=x[j];j++;k++;}
  20.  
  21. for(k=1,i=s;i<=d;k++,i++)
  22. x[i]=v[k];
  23. }
  24.  
  25. void MergeSort(int s, int d)
  26. {
  27. int m;
  28. if(s<d)
  29. {
  30. divizeaza(s,d,m);
  31. MergeSort(s,m);
  32. MergeSort(m+1,d);
  33. interclaseaza(s,d,m);
  34. }}
  35. void main()
  36. {
  37. int i;
  38. cout<<"n=";
  39. cin>>n;
  40. for(i=1;i<=n;i++)  {
  41. cout<<"x["<<i<<"]=";
  42. cin>>x[i];  }
  43. MergeSort(1,n);
  44. cout<<"Vectorul sortat: "<<endl;
  45. for(i=1;i<=n;i++)
  46. cout<<x[i]<<" ";  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement