944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1488
  • C++ RSS
Nov 1st, 2007
0

Merge sort not working

Expand Post »
Below is the code for my merge sort - It doesn't like my temporary array c and I can't get anything to sort - I get all 0 in my output.

C++ Syntax (Toggle Plain Text)
  1. void merge(int low,int mid,int high)
  2. { int size, p, q, i, r;
  3. size = high - low + 1; //size of array c
  4. int c[size]; //array c - the temporary array for sorting
  5. p=low; //index for a[i]...a[mid]
  6. q=mid+1; //index for a[mid + 1]...a[j]
  7. while((p<=mid)&&(q<=high))
  8. { if(a[p]<=a[q]) //copy smaller value to local array c
  9. { c[i]=a[p];
  10. p++;
  11. }
  12. else
  13. { c[i]=a[q];
  14. q++;
  15. }
  16. i++;
  17. }
  18. while (a[p]<= mid)//copy the rest of the longer array
  19. { c[i] = a[p];
  20. }
  21. while (a[q]<=high)//copy the rest of the larger array
  22. { c[i]=a[q];
  23. }
  24. for (i=low; i<=high; i++)//copy back from temp array to main array
  25. { a[i]=c[i];
  26. }}
  27. void merge(int low,int mid,int high)
  28. { int size, p, q, i, r;
  29. size = high - low + 1; //size of array c
  30. int c[size]; //array c - the temporary array for sorting
  31. p=low; //index for a[i]...a[mid]
  32. q=mid+1; //index for a[mid + 1]...a[j]
  33. while((p<=mid)&&(q<=high))
  34. { if(a[p]<=a[q]) //copy smaller value to local array c
  35. { c[i]=a[p];
  36. p++;
  37. }
  38. else
  39. { c[i]=a[q];
  40. q++;
  41. }
  42. i++;
  43. }
  44. while (a[p]<= mid)//copy the rest of the longer array
  45. { c[i] = a[p];
  46. }
  47. while (a[q]<=high)//copy the rest of the larger array
  48. { c[i]=a[q];
  49. }
  50. for (i=low; i<=high; i++)//copy back from temp array to main array
  51. { a[i]=c[i];
  52. }}
  53. int main()
  54. { long timeElapsed;
  55. timeElapsed = clock();
  56. int num,i,elem, percent;
  57. //a[num];
  58. cout<<"*********************************************************************"<<endl;
  59. cout<<" MERGE SORT PROGRAM"<<endl;
  60. cout<<"**********************************************************************"<<endl<<endl<<endl;
  61. cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort[THEN PRESS ENTER]:"<<endl;
  62. cin>>num;
  63. cout<<"Please Enter THE PERCENTAGE you want to sort[THEN PRESS ENTER]:"<<endl;
  64. cin>>percent;
  65. //for (i=1; i<=num; i++)
  66. for (i=0; i<num; i++)
  67. { elem =(rand()+1);
  68. cout<<endl<<"element "<<i<<"is "<<elem<<endl;
  69. a[i]= elem;
  70. cout<<"When i is "<<i<<" ai is "<<a[i];
  71. }
  72. merge_sort(0,num);
  73. cout<<endl<<"So, the sorted list (using MERGE SORT) will be : "<<endl;
  74. for(i=0;i<num;i++)
  75. { cout<<a[i]<<" ";
  76. }
  77. cout<<endl<<endl<<"Time Elapsed is: "<<timeElapsed<<endl<<endl<<endl<<endl;
  78. system("PAUSE");
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mqueene7 is offline Offline
5 posts
since Oct 2007
Nov 1st, 2007
0

Re: Merge sort not working

> ... It doesn't like my temporary array c and I can't get anything to sort ...
the compiler should not like it at all (it is not c++); and should give you an error.
C++ Syntax (Toggle Plain Text)
  1. int c[size]; //array c - the temporary array for sorting
size is not a constant known at compile time.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 1st, 2007
0

Re: Merge sort not working

>..size is not a constant known at compile time<

I don't know what you mean by this - I defined size before I declared my array c. What do I need to change?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mqueene7 is offline Offline
5 posts
since Oct 2007
Nov 1st, 2007
0

Re: Merge sort not working

The size variable is a variable. The compiler does not know what value it might hold at any given time.

Since you are using C++, you should be using a vector or deque for this instead of an array. But if you must use an array, just make one as big as the largest size you think you'll have, and make sure that it complains if size ever gets too large.

I haven't time to look very deeply at your code ATM, so if you are still having problems later I'll help then.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 1st, 2007
0

Re: Merge sort not working

I think you need to go sit down with your professor and work on this a little with him. There are a lot of syntactic and logical errors in here, which I think you can best overcome by working with someone face to face. He shouldn't give you a hard time. Most professors are genuinely pleased when students ask for help understanding a problem. (If for no other reason, it gives them a chance to talk about it more.)

Before you go, google "merge sort" to find some good animations of what is going on. Ignore the stuff in the WikiPedia. (There's nothing wrong with it, it'll just confuse you and your professor will know if you scavenge code from it --honestly, it would be really obvious.)

I was going to give you a bunch of stuff about a binary tree (which is a form of linked list) but I think you ought to go see your professor first. Most merge sorts are done using recursion instead of using an actual binary tree in memory --which is how you are trying to do it.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

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: Fibonacci number series
Next Thread in C++ Forum Timeline: same problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC