(Initial discussion from Classic merge sort, since it is new code, I start a new thread)
Post my code below, my major question is, I have to create another array result to hold sub-parts merge sort result. Is there a way I can just use original number to save additional space in result?
Any other comments on code bugs, performance (in terms of algorithm time complexity), code style, etc. are appreciated.
Code written in Python 2.7.
def merge_sort(numbers, start, end):
if start == end:
return
pivot_index = start + (end-start)//2
merge_sort(numbers, start, pivot_index)
merge_sort(numbers, pivot_index+1, end)
i = start
j = pivot_index+1
result = []
while i <= pivot_index and j <= end:
if numbers[i] <= numbers[j]:
result.append(numbers[i])
i+=1
else:
result.append(numbers[j])
j+=1
if i <= pivot_index:
result.extend(numbers[i:pivot_index+1])
if j <= end:
result.extend(numbers[j:end+1])
k=0
for i in range(start, end+1):
numbers[i] = result[k]
k+=1
if __name__ == "__main__":
numbers = [1,4,2,5,6,8,3,4,0]
merge_sort(numbers, 0, len(numbers)-1)
print numbers
numberlacks "the plural-s") Are you aware of Ford-Johnson merge-insertion sort (and improvements, e.g. by T. D. Bui & Mai Thanh), "Practical in-place mergesort"s by Katajainen, Pasanen & Teuhola (based on Kronrod) or Huang & Langston, and the relatively new, non-stable QuickMergeSort? \$\endgroup\$sure if merge-insertion sort from time complexity perspective, less efficient than [merge sort with a single buffer allocation]well, the attempts at in-place merge sort before "Practical in-place mergesort" were not practical due to increased run time, "the practical ones" have been complicated, QuickMergeSort may not be a merge sort in everybody's book. \$\endgroup\$