Tuesday, August 08, 2006

introduction to algorithms:exercise 2.3-1

here is merge-sort in concise:
merge-sort(A,p,r) //A array to sort,p and r are indices;
__if ( p < r ) then //here we use '_' instead of ' '(white space);
____q = floor((p+r)/2); //take the merge-point as floor of middle position;
____merge-sort(A,p,q); //be careful with the indices;
____merge-sort(A,q+1,r);
____merge(A,p,q,r);
merge-sort end;

now, we will use this algorithm to operate the array A=[3,41,52,26,38,57,9,49];
______________________A=[3,41,52,26,38,57,9,49]
_________________________________|__________________
_____________________|_____________________________|
______________[3,41,52,26]____________________[38,57,9,49]
_____________________|_______________________________|________________
____________|_____________|___________________|______________________|
_________[3,41]________[52,26]_____________[38,57]_________________[9,49]
___________|______________|___________________|______________________|____
______|_________|______|______|____________|______|_______________|______|
_____[3]_______[41]____[52]____[26]__________[38]___[57]_____________[9]____[49]

above is the divide-conquer tree,and the operation sequence in left recurrence is followed:
[3,41,52,26,38,57,9,49]
[3,41,26,52,38,57,9,49]
[3,26,41,52,38,57,9,49]
[3,41,52,26,38,57,9,49]
[3,41,52,26,38,57,9,49]
[3,41,52,26,9,38,49,57]
[3,9,26,38,41,49,52,57]

that is all of the solution.

0 Comments:

Post a Comment

<< Home