茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    幾種Python實現(xiàn)的排序算法
    來源:易賢網(wǎng) 閱讀:975 次 日期:2015-04-15 15:16:20
    溫馨提示:易賢網(wǎng)小編為您整理了“幾種Python實現(xiàn)的排序算法”,方便廣大網(wǎng)友查閱!

    這幾種算法包括了:冒泡排序,插入排序,選擇排序,快速排序和希爾排序,Python版本:2.7.3,具體代碼如下:

    import sys, getopt, random

    def bubble_sort(seq):

    for i in range(len(seq)):

    for j in range(1,len(seq)):

    if seq[j-1]>seq[j]:

    seq[j-1], seq[j]= seq[j], seq[j-1]

    return seq

    def insertion_sort(seq):

    for i in range(1,len(seq)):

    tmp=seq[i]

    pos=i;

    for j in range(i-1,-1,-1):

    if seq[j]>tmp:

    seq[j+1]=seq[j]

    pos=j

    seq[pos]=tmp

    return seq

    def selection_sort(seq):

    for i in range(len(seq)):

    min_index=i;

    for j in range(i,len(seq)):

    if seq[j]<seq[min_index]:

    min_index=j

    seq[i],seq[min_index]=seq[min_index],seq[i]

    return seq

    def partition(seq,p,l,r):

    pivot = seq[p]

    seq[p],seq[r]=seq[r],seq[p]

    p_pos = l

    for i in range(l,r):

    if seq[i]<=pivot:

    seq[i],seq[p_pos]=seq[p_pos],seq[i]

    p_pos=p_pos+1

    seq[p_pos],seq[r]=seq[r],seq[p_pos]

    return p_pos

    def quick_sort(seq,left,right):

    if left < right:

    pivot = random.randint(left,right)

    mid = partition(seq,pivot,left,right)

    quick_sort(seq,left,mid-1)

    quick_sort(seq,mid+1,right)

    return seq

    def shell_sort(seq):

    incr = len(seq)/2

    while(incr>=1):

    for i in range(incr,len(seq)):

    tmp=seq[i]

    pos=i;

    for j in range(i-incr,-1,-incr):

    if seq[j]>tmp:

    seq[j+incr]=seq[j]

    pos=j

    seq[pos]=tmp

    incr = incr/2

    return seq

    def usage():

    print 'Usage: python sort.py sorttype[-q|-i|-b|-s|--shell] sequence'

    print 'Example: python sort.py -q 11,32,3,24,5'

    def main():

    try:

    if(len(sys.argv)==1) or (len(sys.argv)!=3):

    raise Exception()

    else:

    opts,args=getopt.getopt(sys.argv[1:],'qibs',['shell'])

    if len(args)>0:

    seq=[]

    tmp=args[0].split(',')

    for i in tmp:

    seq.append(int(i))

    else:

    raise Exception()

    for opt in opts:

    if opt[0] =='-q':

    print quick_sort(seq,0,len(seq)-1)

    elif opt[0] =='-i':

    print insertion_sort(seq)

    elif opt[0] =='-b':

    print bubble_sort(seq)

    elif opt[0] =='-s':

    print selection_sort(seq)

    elif opt[0] =='--shell':

    print shell_sort(seq)

    except Exception,e:

    usage()

    print e

    sys.exit()

    if __name__ =="__main__":

    main()

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:幾種Python實現(xiàn)的排序算法

    2026上岸·考公考編培訓(xùn)報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)