|
| 1 | +import parse from '../../pseudocode/parse'; |
| 2 | + |
| 3 | +export default parse(` |
| 4 | +\\Note{ This is a copied+modified version of the top down mergesort pseudocode. |
| 5 | +Merge is identical and bookmarks have been left in (but more will be needed |
| 6 | +for the code that is not the same). Ideally the animation sould look as |
| 7 | +similar to the top down version as possible. There is no call stack but |
| 8 | +runlength could be displayed where the top down stack is displayed. |
| 9 | +\\Note} |
| 10 | +
|
| 11 | +\\Code{ |
| 12 | +Main |
| 13 | +// Sort array A[1]..A[size] in ascending order |
| 14 | +Mergesort(A, size) |
| 15 | + runlength <- 1 // each element is a (sorted) run of length 1 |
| 16 | + while runlength < size |
| 17 | + \\Expl{ We stop when the whole array is a single run. |
| 18 | + \\Expl} |
| 19 | + \\In{ |
| 20 | + merge all consecutive pairs of runs of length runlength \\Ref MergeAll |
| 21 | + runlength <- runlength * 2 // merging runs doubles the run length |
| 22 | + \\In} |
| 23 | +\\Code} |
| 24 | +
|
| 25 | +\\Code{ |
| 26 | +MergeAll |
| 27 | + left <- 1 |
| 28 | + while left + runlength < size |
| 29 | + \\Expl{ Unless size is a power of two there can be times when the |
| 30 | + number of runs is odd and we have a "leftover" run at the end |
| 31 | + (with length <= runlength), that will be merged in a later iteration. |
| 32 | + \\Expl} |
| 33 | + \\In{ |
| 34 | + mid <- left + runlength - 1 // first run is A[left..mid] |
| 35 | + right <- minimum(mid+runlength, size) // next is A[mid+1..right] |
| 36 | + \\Expl{ The rightmost run in A may be shorter than runlength |
| 37 | + \\Expl} |
| 38 | + merge A[left..mid] and A[mid+1..right], with the result in A \\Ref MergeCopy |
| 39 | + left <- right + 1 // skip to the next pair of runs (if any) |
| 40 | + \\In} |
| 41 | +\\Code} |
| 42 | +
|
| 43 | +\\Note{ |
| 44 | +XXXXXXXXXXXXXXXXXXXXXXXXXXXXX following verbatim from top-down mergesort |
| 45 | +\\Note} |
| 46 | +
|
| 47 | +\\Code{ |
| 48 | +MergeCopy |
| 49 | + Merge(A, left, mid, right, B) \\Ref Merge |
| 50 | + \\Expl{ Takes two sorted array segments, A[left..mid] and A[mid+1..right], |
| 51 | + and merges them together to form a single sorted array segment |
| 52 | + in temporary array B[left..right]. |
| 53 | + The animation shows values being deleted from A since they |
| 54 | + are no longer needed (they are actually still there). |
| 55 | + \\Expl} |
| 56 | + Copy merged elements back to A \\B copyBA |
| 57 | + \\Expl{ Copy elements from B[left..right] back to A[left..right]. |
| 58 | + Copying can be reduced by merging |
| 59 | + from A to B and from B to A in alternate levels of recursion - |
| 60 | + a slightly more tricky coding. |
| 61 | + The animation shows values being deleted from B since they |
| 62 | + are no longer needed (they are actually still there). |
| 63 | + \\Expl} |
| 64 | + \\Note{ Might be better to move above to overview. |
| 65 | + \\Note} |
| 66 | +\\Code} |
| 67 | +
|
| 68 | +\\Code{ |
| 69 | +Merge |
| 70 | + ap1 <- left \\B ap1 |
| 71 | + max1 <- mid \\B max1 |
| 72 | + \\Expl{ ap1 scans through the segment A[left..mid], "pointing at" or |
| 73 | + indexing elements of this array segment we copy from. |
| 74 | + \\Expl} |
| 75 | + ap2 <- max1+1 \\B ap2 |
| 76 | + max2 <- right \\B max2 |
| 77 | + \\Expl{ ap2 scans through the segment A[mid+1..right], "pointing at" or |
| 78 | + indexing elements of this array segment we copy from. |
| 79 | + \\Expl} |
| 80 | + bp <- ap1 \\B bp |
| 81 | + \\Expl{ bp scans through the segment B[left..right], "pointing at" or |
| 82 | + indexing elements of this array segment we copy to. |
| 83 | + \\Expl} |
| 84 | + while both A segments still have elements to copy \\Ref MergeWhile |
| 85 | + \\Expl{ we scan through both A segments from left to right by |
| 86 | + incrementing ap1 and ap2, copying to B as we go. |
| 87 | + The animation shows values being deleted from A since they |
| 88 | + are no longer needed (they are actually still there). |
| 89 | + \\Expl} |
| 90 | + \\In{ |
| 91 | + copy the smaller A element, increment its pointer and bp \\Ref CopySmaller |
| 92 | + \\Expl{ The smaller of A[ap1] and A[ap2] is copied to B[bp]. |
| 93 | + \\Expl} |
| 94 | + \\In} |
| 95 | + copy any remaining elements from A to B \\Ref CopyRest |
| 96 | + \\Expl{ One of the A segments will have been completely copied; |
| 97 | + the other has uncopied elements. |
| 98 | + \\Expl} |
| 99 | +\\Code} |
| 100 | +
|
| 101 | +\\Code{ |
| 102 | +MergeWhile |
| 103 | + while ap1 <= max1 and ap2 <= max2 \\B MergeWhile |
| 104 | + \\Expl{ Elements up to max1/max2 must be copied; those before |
| 105 | + ap1/ap2 have been copied already. |
| 106 | + \\Expl} |
| 107 | +\\Code} |
| 108 | +
|
| 109 | +\\Code{ |
| 110 | +CopySmaller |
| 111 | + if A[ap1] < A[ap2] \\B findSmaller |
| 112 | + \\In{ |
| 113 | + B[bp] <- A[ap1] \\B copyap1 |
| 114 | + \\Expl{ The animation shows the value being deleted from A[ap1] since it |
| 115 | + is no longer needed (it is actually still there). |
| 116 | + \\Expl} |
| 117 | + ap1 <- ap1+1 \\B ap1++ |
| 118 | + bp <- bp+1 \\B bp++ |
| 119 | + \\Note{ Clearer to duplicate this in then and else branches(?) |
| 120 | + \\Note} |
| 121 | + \\In} |
| 122 | + else |
| 123 | + \\In{ |
| 124 | + B[bp] <- A[ap2] \\B copyap2 |
| 125 | + \\Expl{ The animation shows the value being deleted from A[ap2] since it |
| 126 | + is no longer needed (it is actually still there). |
| 127 | + \\Expl} |
| 128 | + ap2 <- ap2+1 \\B ap2++ |
| 129 | + bp <- bp+1 \\B bp++_2 |
| 130 | + \\In} |
| 131 | +\\Code} |
| 132 | +
|
| 133 | +\\Code{ |
| 134 | +CopyRest |
| 135 | + copy A[ap1..max1] to B[bp..] \\B CopyRest1 |
| 136 | + \\Note{ Need to expand this? I dont think so. |
| 137 | + \\Note} |
| 138 | + copy A[ap2..max2] to B[bp..] \\B CopyRest2 |
| 139 | + \\Expl{ One of these copy steps will do nothing because one of the |
| 140 | + A segments will be empty. If ap2 is not shown in the animation |
| 141 | + it is max2+1, off the end of the array. |
| 142 | + The animation shows values being deleted from A since they |
| 143 | + are no longer needed (they are actually still there). |
| 144 | + \\Expl} |
| 145 | +\\Code} |
| 146 | +
|
| 147 | +`); |
0 commit comments