#! /usr/bin/python
#coding=utf-8
#合并排序
def merge(array,p,q,r):
#print p,q,r
n1=q-p+1
n2=r-q;
L=[]
R=[]
for index in range(n1):
L.append(array[p-1+index])
L.append(float("inf")) #float("inf"表示无穷大数
#print L
for index in range(n2):
R.append(array[q+index])
R.append(float("inf"))
#print R
i=0
j=0
for k in xrange(p-1,r):
if L[i]<=R[j]:
array[k]=L[i]
i=i+1
else:
array[k]=R[j]
j=j+1
def merge_sort(array,p,r):
if p<r:
q=(p+r)/2
merge_sort(array,p,q)
merge_sort(array,q+1,r)
merge(array,p,q,r)
def main():
listarray=[100,55,12,89,0,7,6,8,88,55,4,1,5,10,4,3]
merge_sort(listarray,1,len(listarray))
print listarray
if __name__ == "__main__":
main()
#coding=utf-8
#合并排序
def merge(array,p,q,r):
#print p,q,r
n1=q-p+1
n2=r-q;
L=[]
R=[]
for index in range(n1):
L.append(array[p-1+index])
L.append(float("inf")) #float("inf"表示无穷大数
#print L
for index in range(n2):
R.append(array[q+index])
R.append(float("inf"))
#print R
i=0
j=0
for k in xrange(p-1,r):
if L[i]<=R[j]:
array[k]=L[i]
i=i+1
else:
array[k]=R[j]
j=j+1
def merge_sort(array,p,r):
if p<r:
q=(p+r)/2
merge_sort(array,p,q)
merge_sort(array,q+1,r)
merge(array,p,q,r)
def main():
listarray=[100,55,12,89,0,7,6,8,88,55,4,1,5,10,4,3]
merge_sort(listarray,1,len(listarray))
print listarray
if __name__ == "__main__":
main()