[Python]插入排序,快速排序,冒泡排序

來源:本網整理

#encoding=utf-8import randomfrom copy import copydef directInsertSort(seq):直接插入排序""size=len(seq)for i in range(1,size):tmp,j=seq[i],iwhile j>0 and tmp[j-1]:seq[j],j=seq[j-1],j-1seq[j]=tmpreturn seqdef directSelectSort(seq):直接選擇排序""size=len(seq)for i in range(0,size-1):k=i;j=i+1while jif seq[j][k]:k=jj+1seq[i],seq[k]=seq[k],seq[i]return seqdef bubbleSort(seq):冒泡排序""size=len(seq)for i in range(1,size):for j in range(0,size-i):if seq[j+1][j]:seq[j+1],seq[j]=seq[j],seq[j+1]return seqdef_divide(seq,low,high):快速排序劃分函數""tmp=seq[low]while low!high:while low[high]>=tmp:high-=1if lowseq[low]=seq[high]low+1while low[low]if lowseq[high]=seq[low]high-=1seq[low]=tmpreturn lowdef_quickSort(seq,low,high):快速排序輔助函數""if low>=high:returnmid=_divide(seq,low,high)quickSort(seq,low,mid-1)quickSort(seq,mid+1,high)def quickSort(seq):快速排序包裹函數""size=len(seq)quickSort(seq,0,size-1)return seqdef merge(seq,left,mid,right):tmp=[]i,j=left,midwhile iif seq[i][j]:tmp.append(seq[i])i+1else:tmp.append(seq[j])j+1if i(seq[i:])if j(seq[j:])seq[left:right+1]=tmp[0:right-left+1]def_mergeSort(seq,left,right):if left=right:returnelse:mid=(left+right)/2mergeSort(seq,left,mid)mergeSort(seq,mid+1,right)merge(seq,left,mid+1,right)二路并歸排序def mergeSort(seq):size=len(seq)mergeSort(seq,0,size-1)return seqif_name_='_main_':s=[random.randint(0,100)for i in range(0,20)]print sprint"\\nprint directSelectSort(copy(s))print directInsertSort(copy(s))print bubbleSort(copy(s))print quickSort(copy(s))print mergeSort(copy(s))www.anxorj.tw防采集請勿采集本網。

就是寫了腦圖式的代碼,沒有做任何代碼重構,不喜勿噴!謝謝 scrolltotop.offset(100,120); scrolltotop.init();

[1].[代碼] [Python]代碼 跳至[1]

#encoding=utf-8'''Created on [email protected]: cooler'''import sys,os,timereload(sys)sys.setdefaultencoding('utf8')def qsort(l,i,j): a ,b =i,j tmp = l[i] while i<j: if l[j] < tmp: l[i] = l[j] i += 1 l[j]=l[i] else: j -= 1 l[i] = tmp if a<i: qsort(l,a,i) if i+1<b: qsort(l,i+1,b) # print ldef isort(l): for x in range(len(l)): tmp_index = x for y in range(x+1,len(l)): if l[tmp_index] > l[y]: tmp_index = y l[tmp_index], l[x]= l[x],l[tmp_index] # return ldef msort(l): for i in range(len(l)-1,0,-1): for x in range(i): if l[x] > l[x+1]: l[x],l[x+1] = l[x+1],l[x] # print lif __name__ == '__main__': l = [42,2,3,6,98,6,8,2,44,35,62,3] print l qsort(l,0,len(l)-1) print l isort(l) print l msort(l) print l SyntaxHighlighter.autoloader( 'applescript/js/sh309/scripts/shBrushAppleScript.js?t=1451961936000', 'actionscript3 as3/js/sh309/scripts/shBrushAS3.js?t=1451961936000', 'bash shell/js/sh309/scripts/shBrushBash.js?t=1451961936000', 'coldfusion cf/js/sh309/scripts/shBrushColdFusion.js?t=1451961936000', 'cpp c/js/sh309/scripts/shBrushCpp.js?t=1451961936000', 'obj-c objc/js/sh309/scripts/shBrushObjC.js?t=1451961936000', 'c# c-sharp csharp/js/sh309/scripts/shBrushCSharp.js?t=1451961936000', 'css/js/sh309/scripts/shBrushCss.js?t=1451961936000', 'delphi pascal/js/sh309/scripts/shBrushDelphi.js?t=1451961936000', 'diff patch pas/js/sh309/scripts/shBrushDiff.js?t=1451961936000', 'erl erlang/js/sh309/scripts/shBrushErlang.js?t=1451961936000', 'groovy/js/sh309/scripts/shBrushGroovy.js?t=1451961936000', 'haxe hx/js/sh309/scripts/shBrushHaxe.js?t=1451961936000', 'java/js/sh309/scripts/shBrushJava.js?t=1451961936000', 'jfx javafx/js/sh309/scripts/shBrushJavaFX.js?t=1451961936000', 'js jscript javascript/js/sh309/scripts/shBrushJScript.js?t=1451961936000', 'perl pl/js/sh309/scripts/shBrushPerl.js?t=1451961936000', 'php/js/sh309/scripts/shBrushPhp.js?t=1451961936000', 'text plain/js/sh309/scripts/shBrushPlain.js?t=1451961936000', 'py python/js/sh309/scripts/shBrushPython.js?t=1451961936000', 'ruby rails ror rb/js/sh309/scripts/shBrushRuby.js?t=1451961936000', 'scala/js/sh309/scripts/shBrushScala.js?t=1451961936000', 'sql/js/sh309/scripts/shBrushSql.js?t=1451961936000', 'vb vbnet/js/sh309/scripts/shBrushVb.js?t=1451961936000', 'xml xhtml xslt html/js/sh309/scripts/shBrushXml.js?t=1451961936000' ); SyntaxHighlighter.all();

1、首先我們定義一個列表輸入一串大小不一的數字。2、可以用sort()方法對定義的列表排序,注意,sort只是對列表排序,它沒有返回一個值。3、輸入print列表名即可得到排序后的列表數據。4、倒序

你縮進有問題吧,你可以加下注釋嗎內容來自www.anxorj.tw請勿采集。

免責聲明 - 關于我們 - 聯系我們 - 廣告聯系 - 友情鏈接 - 幫助中心 - 頻道導航
Copyright © 2017 www.anxorj.tw All Rights Reserved
陕西快乐10分下载