943,903 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 897
  • C++ RSS
Feb 9th, 2008
0

merge sort not working

Expand Post »
i am trying out merge sort.
i have made two functions sort and merge;
i am calling merge recursively.
i think my sort function is working fine. the problem lies with merge function.
in merge function i am dividing the array using recursive calls. it ends when lb = = ub(ie lower bound equals upper bound). lower bound and upper bound are index of array.

then i am passing it to sort function function.

suggest me a solution of correction to merge sort.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include<iostream>
  3. //#include<stream>
  4.  
  5. using namespace std;
  6.  
  7. int A[7] = {3,4,77,10,2,5,7};
  8.  
  9. void sort(int l1,int u1,int l2,int u2)
  10. {
  11. int B[7];
  12. int i=l1;
  13. int j=l2;
  14. int k=0;
  15.  
  16. while(i<=u1&&j<=u2) {
  17. if(A[i]<A[j]) {
  18. B[k] = A[i];
  19. i++;
  20. k++;
  21. }
  22.  
  23. else {
  24. B[k] = A[j];
  25. j++;
  26. k++;
  27. }
  28. }
  29.  
  30.  
  31. while(j<=u2) {
  32. B[k]=A[j];
  33. j++;
  34. k++;
  35. }
  36.  
  37. while(i<=u1) {
  38. B[k] = A[i];
  39. i++;
  40. k++;
  41. }
  42.  
  43. for(i=l1;i<=u2;i++){
  44. A[i]=B[i];
  45. }
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. void merge(int lb,int ub)
  53. {
  54. int lo;
  55. lo=(ub+lb)/2;
  56.  
  57. if(lb<ub){
  58. merge(lb,lo);
  59. merge(lo+1,ub);
  60. sort(lb,lo,lo+1,ub);
  61. }
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67. for(int i=0;i<7;i++)
  68. cout<<A[i]<<" ";
  69. cout<<endl;
  70.  
  71. merge(0,6);
  72. //sort(0,3,4,6);
  73. for(int i=0; i<7;i++)
  74. cout<<A[i]<<" ";
  75.  
  76. return 0;
  77. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
TSharma is offline Offline
7 posts
since Feb 2008
Feb 9th, 2008
0

Re: merge sort not working

I don't think it is your merge function but the sort function.
You're starting to fill in B in the beginning but you have to start at l1.
So the inititial value for k has to be l1, not zero.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wouterdc is offline Offline
4 posts
since Feb 2008
Feb 13th, 2008
0

Re: merge sort not working

Thanks i just got what is wrong!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
TSharma is offline Offline
7 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to pause a timer
Next Thread in C++ Forum Timeline: ADT Calendar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC