Merge sort not working

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

Join Date: Oct 2007
Posts: 5
Reputation: mqueene7 is an unknown quantity at this point 
Solved Threads: 0
mqueene7 mqueene7 is offline Offline
Newbie Poster

Merge sort not working

 
0
  #1
Nov 1st, 2007
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.

  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");
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Merge sort not working

 
0
  #2
Nov 1st, 2007
> ... 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.
  1. int c[size]; //array c - the temporary array for sorting
size is not a constant known at compile time.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: mqueene7 is an unknown quantity at this point 
Solved Threads: 0
mqueene7 mqueene7 is offline Offline
Newbie Poster

Re: Merge sort not working

 
0
  #3
Nov 1st, 2007
>..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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Merge sort not working

 
0
  #4
Nov 1st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Merge sort not working

 
0
  #5
Nov 1st, 2007
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.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC