1: def mergesort(seq):
2: if len(seq)<=1:
3: return seq
4: mid=len(seq)/2
5: left=mergesort(seq[:mid])
6: right=mergesort(seq[mid:])
7: return merge(left,right)
8:
9: def merge(left,right):
10: result=[]
11: i,j=0,0
12: while i<len(left) and j<len(right):
13: if left[i]<=right[j]:
14: result.append(left[i])
15: i+=1
16: else:
17: result.append(right[j])
18: j+=1
19: result+=left[i:]
20: result+=right[j:]
21: return result
22:
23: if __name__==‘__main__‘:
24: seq=[4,5,7,9,7,5,1,0,7,-2,3,-99,6]
25: print(mergesort(seq))
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
时间: 2024-10-18 04:42:01