Can any one solve the problem with this merge sort??????????

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 3
Reputation: sanzle is an unknown quantity at this point 
Solved Threads: 0
sanzle sanzle is offline Offline
Newbie Poster

Can any one solve the problem with this merge sort??????????

 
0
  #1
Feb 12th, 2008
  1. #include<iostream>
  2. using namespace std;
  3. void merge(int a[],int low,int mid,int high);
  4. void mergesort(int a[],int low, int high)
  5. {
  6. if(high>low){
  7. int mid=(low+high)/2;
  8. mergesort(a,low,mid);
  9. mergesort(a,(mid+1),high);
  10. merge(a,low,mid,high);
  11. }
  12. }
  13.  
  14. void merge(int a[],int low,int mid,int high){
  15.  
  16. int t[(high-low)+1];
  17. int il=0;
  18. int ir=0;
  19. int nl = mid - low + 1;
  20. int nr = high - mid;
  21. while((il<nl)&&(ir<nr)){
  22. if(a[low+il]<=a[mid+1+ir]){
  23. t[il+ir]=a[low+il];
  24. il++;
  25. }
  26. else{
  27. t[il+ir]=a[mid+1+ir];
  28. ir++;
  29. }
  30. }
  31. while(il<nl){
  32. t[il+ir]=a[low+il];
  33. il++;
  34. }
  35. while(ir<nr){
  36. t[il+ir]=a[mid+1+ir];
  37. ir++;
  38. }
  39. for(int i=low;i<high;i++){
  40. a[i]=t[i-low];
  41. }
  42.  
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48. int a[10] = {0,4,9,11,16,2,3,5,7,8};
  49. mergesort(a,0,10);
  50. for(int i=0;i<10;i++)
  51. cout<<a[i]<<" ";
  52. cout<<endl;
  53. return 0;
  54. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,853
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Can any one solve the problem with this merge sort??????????

 
0
  #2
Feb 12th, 2008
What work and doesn't work? What's the problem?

One thing I see is:
int t[(high-low)+1]; You can't declare array this way. high and low aren't const int.

Niek
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: Can any one solve the problem with this merge sort??????????

 
0
  #3
Feb 12th, 2008
You really think that we can read this code without comments?
An Apple a Day keeps a Doctor away!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC