|  | 
|  | 1 | +# Copyright 2013, Michael H. Goldwasser | 
|  | 2 | +# | 
|  | 3 | +# Developed for use with the book: | 
|  | 4 | +# | 
|  | 5 | +#    Data Structures and Algorithms in Python | 
|  | 6 | +#    Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser | 
|  | 7 | +#    John Wiley & Sons, 2013 | 
|  | 8 | +# | 
|  | 9 | +# This program is free software: you can redistribute it and/or modify | 
|  | 10 | +# it under the terms of the GNU General Public License as published by | 
|  | 11 | +# the Free Software Foundation, either version 3 of the License, or | 
|  | 12 | +# (at your option) any later version. | 
|  | 13 | +# | 
|  | 14 | +# This program is distributed in the hope that it will be useful, | 
|  | 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 17 | +# GNU General Public License for more details. | 
|  | 18 | +# | 
|  | 19 | +# You should have received a copy of the GNU General Public License | 
|  | 20 | +# along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|  | 21 | + | 
|  | 22 | +import math | 
|  | 23 | + | 
|  | 24 | +def merge(src, result, start, inc): | 
|  | 25 | +  """Merge src[start:start+inc] and src[start+inc:start+2*inc] into result.""" | 
|  | 26 | +  end1 = start+inc                        # boundary for run 1 | 
|  | 27 | +  end2 = min(start+2*inc, len(src))       # boundary for run 2 | 
|  | 28 | +  x, y, z = start, start+inc, start       # index into run 1, run 2, result | 
|  | 29 | +  while x < end1 and y < end2: | 
|  | 30 | +    if src[x] < src[y]: | 
|  | 31 | +      result[z] = src[x] | 
|  | 32 | +      x += 1 | 
|  | 33 | +    else: | 
|  | 34 | +      result[z] = src[y] | 
|  | 35 | +      y += 1 | 
|  | 36 | +    z += 1                                # increment z to reflect new result | 
|  | 37 | +  if x < end1: | 
|  | 38 | +    result[z:end2] = src[x:end1]          # copy remainder of run 1 to output | 
|  | 39 | +  elif y < end2: | 
|  | 40 | +    result[z:end2] = src[y:end2]          # copy remainder of run 2 to output | 
|  | 41 | + | 
|  | 42 | +def merge_sort(S): | 
|  | 43 | +  """Sort the elements of Python list S using the merge-sort algorithm.""" | 
|  | 44 | +  n = len(S) | 
|  | 45 | +  logn = math.ceil(math.log(n,2)) | 
|  | 46 | +  src, dest = S, [None] * n               # make temporary storage for dest | 
|  | 47 | +  for i in (2**k for k in range(logn)):   # pass i creates all runs of length 2i | 
|  | 48 | +    for j in range(0, n, 2*i):            # each pass merges two length i runs | 
|  | 49 | +      merge(src, dest, j, i) | 
|  | 50 | +    src, dest = dest, src                 # reverse roles of lists | 
|  | 51 | +  if S is not src: | 
|  | 52 | +    S[0:n] = src[0:n]                     # additional copy to get results to S | 
0 commit comments