题目说的有点繁杂
函数实现的功能就是为一个包含list元素的list指定偏移的关键字及次关键字进行升序排序 可以算是实现第一周第一个作业的可能会需要的函数 贡献给大家:有不足的地方希望大家多多指正!
def quicksort(A,s,e,keyexist=False,keybaise=0,keybaiseminor=0):
'''Assume that A must be a list,and the s ->start position,e->end position'''
if s<e:
mid=partition(A,s,e,keyexist,keybaise,keybaiseminor)
quicksort(A,s,mid-1,keyexist,keybaise)
quicksort(A,mid+1,e,keyexist,keybaise)
return
def partition(A,s,e,keyexist=False,keybaise=0,keybaiseminor=0):
'''Assume that A must be a list,and the s ->start position,e->end position'''
if keyexist:
midV=A[e][keybaise]
midVminor=A[e][keybaiseminor]
else:
midV=A[e]
count=0
start=s
end=e-1
if keyexist:
while start<=end:
if midV>A[start][keybaise]:
start+=1
elif midV==A[start][keybaise]:
if midVminor>A[start][keybaiseminor]:
start+=1
else:
exchange(A,start,end)
end-=1
else:
exchange(A,start,end)
end-=1
else:
while start<=end:
if midV>A[start]:
start+=1
else:
exchange(A,start,end)
end-=1
exchange(A,e,start)
return start
def exchange(A,x,y,keyexist=False):
'''the fouth parament only adapt for A[x] only contain simple elements'''
if keyexist:
temp=A[x][:]
A[x]=A[y][:]
A[y]=temp[:]
else:
temp=A[x]
A[x]=A[y]
A[y]=temp
return
好啦 这个本来是coursera上课程第一周作业的相关程序
更新一下 发现使用python自带的sort函数就可以完成上面的排序功能
x.sort(key=lambda x:(x[0],x[1]))
以上