This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursion and MergeSort : How is it working ?

 
Ashish Maharaja Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a program to do MergeSort. It is not ready yet. Here is the test code which can split an array into half - works properly.
But I dont understand how it works - can someone explain this in terms of Stacks etc ?
Please let me know if my way is good or not.

What i am trying :
MyArray class has an int array called FULL. It stores the Left and Right halves of FULL in its Lt and Rt variables respectively.
It takes the array to be sorted and splits it into halves till array of size 1 is obtained. We (should) store the bottom-most array reference so
that we can go upwards (by using the MyArray "above" variable) and do the sorting.



 
Ashish Schottky
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can go here: to check how merge sort works and how it is implemented:
Programing AI
 
Ashish Maharaja Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Schottky wrote:You can go here: to check how merge sort works and how it is implemented:
Programing AI


Thanks for the link. I prefer to do it myself. If there are any serious flaws in my approach, please let me know.
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pencil, paper and a large eraser. That is what you need. You need to write down exactly how you intend to implement the splitting part of the algorithm, and how you stop when your divided arrays are 1 element long. Then you need to write down how to implement the merging part. When you have got it down to words of one syllable, you can convert your instructions into code quite easily.

I would suggest some more links about sorting: 1 2 3.
 
Ashish Maharaja Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Pencil, paper and a large eraser. That is what you need. You need to write down exactly how you intend to implement the splitting part of the algorithm, and how you stop when your divided arrays are 1 element long. Then you need to write down how to implement the merging part. When you have got it down to words of one syllable, you can convert your instructions into code quite easily.

I would suggest some more links about sorting: 1 2 3.


Thanks for the links. I am trying this again. Lots of paper in the bin already
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're supposed to rub out your errors, saving you throwing lots of paper away.

Or maybe the paper is where you wrote down your comments and opinions
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic