First question here, and yes this is a homework question. We are tasked with performing merge sort on an array (which I am familiar with), but in a way I am unsure of how to do. Usually I would have a separate merge and merge sort function, and use the two. However, it sounds like he wants everything in one method? I was just hoping maybe someone could help clear things up for me, or put them into terms I can better understand.
From the assignment:
you will need to implement a non-recursive version of merge-sort
algorithm. Arrange two nested loops to accomplish this task. The outer
loop should provide the size of segments for merging. The inner loop
should take care of selecting positions of segments. The inner loop
should start at the left edge and move your segments to the right.
Arrange appropriate values of variables left, middle, right, so that
sorting is accomplished just by iterating the call
merge(a,left,middle,right).
I hate to be so vague, but I really don’t understand any of what he’s saying. First, what is meant by “outer loop should provide the size of segments”? How does a loop provide anything? What about the next sentence – what does he mean by segments? The data?
I’m not asking for code, but any psuedocode would be really helpful.
If anyone could try and decipher what he means, I’d appreciate it. I’ve already emailed him about it, but it’s been a few hours and I’ve yet to hear back.
Thank you!
It’s not so difficult. Consider the recursive merge:
If you notice, when you split, you don’t really do anything. You just tell the recursive function to partially sort the array. Sorting the array consists of first sorting both halves and then merging it. So basically, what you have is this:
Now from here it should be obvious. You first merge elements of the array 2 by 2, then 4 by 4, then 8 by 8 etc. That is the outer
forgives you 2, 4, 8, 16, 32, … (which is what it calls size of the segment because theiof the loop contains that number) and the innerfor(say with iteratorj) goes over the array,ibyimergingarray[j...j+i/2-1]witharray[j+i/2..j+i-1].I wouldn’t write the code since this is homework.
Edit: a picture of how the inner
forworksImagine if
iis 4, so you are at this stage:you will have a
forthat once gives you0(which is0*i) asjand then4(which is1*i) asj. (ifiwas 2, you would havejgoing like 0, 2, 4, 6)Now, once you need to merge
array[0..1]witharray[2..3](which is formulated byarray[j..j+i/2-1]andarray[j+i/2..j+i-1]withj = 0) and thenarray[4..5]witharray[6..7](which is formulated by the same formulasarray[j...j+i/2-1]andarray[j+i/2..j+i-1]because nowj = 4) That is:Hope this is clear at least a little.
Side help: Just a hint if you don’t really know how
forworks:is like writing
So, if you always wrote
it meant starting from 0, while
iis smaller than 10, do something withiand then increment it. Now if you wantito change differently, you just changestatement2such as:(Try to understand how that final
forworks based on its equivalentwhilethat I wrote you)