Algorithm - Understanding Merge Sort Optimization - Avoiding Copies - Stack Overflow
Algorithm - Understanding Merge Sort Optimization - Avoiding Copies - Stack Overflow
Dismiss
I have below merge sort program in algorithms book, it is mentioned that The main problem is
that merging two sorted lists requires linear extra memory, and the additional work spent copying
4 to the temporary array and back, throughout the algorithm, has the effect of slowing down the sort
considerably. This copying can be avoided by judiciously switching the roles of "a" and
"tmp_array" at alternate levels of the recursion.
My question is what does author mean "copying can be avoided by judiciously switching the roles
1 of a and tmp_array at alternate levels of the recursion" and how it is possible in following code?
Request to show an example how we can achieve this?
input_type *tmp_array;
tmp_array = (input_type *) malloc( (n+1) * sizeof (input_type) );
m_sort( a, tmp_array, 1, n );
free( tmp_array );
}
void m_sort( input_type a[], input_type tmp_array[ ], int left, int right ) {
int center;
if( left < right ) {
void merge( input_type a[ ], input_type tmp_array[ ], int l_pos, int r_pos, int
right_end ) {
/* main loop */
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service
while(. ( 1_pos <= left_end ) && ( r_pos <= right_end ) )
https://stackoverflow.com/questions/7577825/understanding-merge-sort-optimization-avoiding-copies 1/6
4/13/2020 algorithm - Understanding merge sort optimization: avoiding copies - Stack Overflow
algorithm
I'm going to assume that, without looking at this code, it is performing merge sort by declaring a
contiguous block of memory the same size as the original.
9 So normally merge sort is like this:
I'm assuming it's recursive, so no copies will be done before we're sorting sub-arrays of size 2.
Now what happens?
_ means it is memory we have available, but we don't care about the data in it
original:
8 5 2 3 1 7 4 6
_ _ _ _ _ _ _ _
recursive call 1:
(8 5 2 3) (1 7 4 6)
By using our site, you acknowledge that you have read_and understand our Cookie Policy, Privacy Policy, and
_ _ _ _ _ _ _
our Termsrecursive
of Service.call 2:
https://stackoverflow.com/questions/7577825/understanding-merge-sort-optimization-avoiding-copies 2/6
4/13/2020 algorithm - Understanding merge sort optimization: avoiding copies - Stack Overflow
((8 5) (2 3)) ((1 7) (4 6))
_ _ _ _ _ _ _ _
recursive call 3:
(((8) (5))((2) (3)))(((1) (7))((4) (6)))
_ _ _ _ _ _ _ _
What the author is suggesting Recursive calls resolving with merging, WITHOUT COPYING
(faster method):
There you go: you don't need to do copies as long as you perform each "level" of the merge-sort
tree in lock-step, as shown above.
You may have a minor issue of parity, also as demonstrated above. That is, your result may be in
your temp_array . You either have three options for dealing with this:
returning the temp_array as the answer, and release the old memory (if your application is
fine with that)
By using our site, you acknowledge
perform thatcopy
a single array you have read and
operation, to understand our Cookie
copy temp_array backPolicy
into, your
Privacy Policy,array
original and
our Terms of Service.
https://stackoverflow.com/questions/7577825/understanding-merge-sort-optimization-avoiding-copies 3/6
4/13/2020 algorithm - Understanding merge sort optimization: avoiding copies - Stack Overflow
allow yourself to consume a mere twice-as-much memory, and perform a single cycle of
merges from temp_array1 to temp_array2 then back to original_array , then release
temp_array2 . The parity issue should be resolved.
1: Merge each consecutive pair of sequences from A0, constructing a new temporary array
A1.
2: Merge each consecutive pair of sequences from A1, constructing a new temporary array
A2.
...
Now, you can obviously get away with just a single temporary array by doing this:
1: Merge each consecutive pair of sequences from A0, constructing a new temporary array
A1.
2: Merge each consecutive pair of sequences from A1, overwriting A0 with the result.
3: Merge each consecutive pair of sequences from A0, overwriting A1 with the result.
...
Of course, you can be even smarter than this. If you want to be nicer to the cache, you might
decide to sort top-down, rather than bottom-up. In this case, it hopefully becomes obvious what
your textbook means when it refers to tracking the role of the arrays at different levels of
recursion.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
Here
our Terms is my.implementation without extra copies.
of Service
https://stackoverflow.com/questions/7577825/understanding-merge-sort-optimization-avoiding-copies 4/6
4/13/2020 algorithm - Understanding merge sort optimization: avoiding copies - Stack Overflow
/**
* Sorts input and returns inversions number
* using classical divide and conquer approach
*
* @param input Input array
* @param start Start index
* @param end End index
* @return int
*/
private static long mergeSort(ArrayList<Integer> input, int start, int end) {
if (end - start < 1) {
return 0;
}
long inversionsNumber = 0;
return inversionsNumber;
}
https://stackoverflow.com/questions/7577825/understanding-merge-sort-optimization-avoiding-copies 5/6
4/13/2020 algorithm - Understanding merge sort optimization: avoiding copies - Stack Overflow
abguy
693 8 12
Look at the very last part of the merge function. What if, instead of copying that data, you just
used the knowledge that the sorted part is now in tmp_array instead of a when the function
0 returns, and a is available for use as a temp.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.
https://stackoverflow.com/questions/7577825/understanding-merge-sort-optimization-avoiding-copies 6/6