Python實(shí)現(xiàn)排序方法常見的四種
1.冒泡排序,相鄰位置比較大小,將比較大的(或小的)交換位置
def maopao(a):
for i in range(0,len(a)):
for j in range(0,len(a)-i-1):
if a[j]>a[j+1]:
temp = a[j+1]
a[j+1] = a[j]
a[j] = temp
#print(a)
#print(a)
print(a)
2.選擇排序,遍歷選擇一個(gè)最小的數(shù)與當(dāng)前循環(huán)的第一個(gè)數(shù)交換
def xuanze(a):
for i in range(0,len(a)):
k=i
temp = a[i]
for j in range(i,len(a)):
if a[j]<temp:
temp = a[j]
k = j
a[k] = a[i]
a[i] = temp
print(a)
3.快速排序:將子段的第一個(gè)元素做為中值,先從右向左遍歷,如過(guò)比中值大high-1,如果比中值小,將這個(gè)值放到low那里。
然后從左向右開始遍歷,如果左側(cè)的比中值大,將他放到high那里。當(dāng)low>=high時(shí),將中值的值賦給low
(1.以下為參照公眾號(hào)中的做法:
a =[7,1,3,2,6,54,4,4,5,8,12,34]
def sort(a,low,high):
while low < high:
temp = a[low]
while low < high and a[high]>=temp:
high = high-1
a[low]=a[high]
while low<high and a[low]<temp:
low = low+1
a[high]=a[low]
a[low]=temp
return low
def quicksort(a,low,high):
if low<high:
middle = sort(a,low,high)
quicksort(a,low,middle)
quicksort(a,middle+1,high)
print(a)
sort(a,0,len(a)-1)
quicksort(a,0,len(a)-1)
print(a)
(2.以下是參照網(wǎng)上的做法:
在做快速排序時(shí)一直各種問(wèn)題,是因?yàn)榈毓衲抢餂](méi)有考慮清楚,一直把low的值賦值為0了,實(shí)際上應(yīng)該是不固定的low值,他每個(gè)子循環(huán)不定。
'''
遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群:531509025
尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書!
'''
a =[7,1,3,2,6,54,4,4,5,8,12,34]
def sort(a,low,high):
while low < high:
temp = a[low]
while low < high and a[high]>=temp:
high = high-1
while low<high and a[high]<temp:
a[low]=a[high]
low =low+1
a[high]=a[low]
a[low]=temp
return low
def quicksort(a,low,high):
if low<high:
middle = sort(a,low,high)
quicksort(a,low,middle)
quicksort(a,middle+1,high)
print(a)
sort(a,0,len(a)-1)
quicksort(a,0,len(a)-1)
print(a)
4.插入排序:從左向右遍歷,依次選取數(shù)值,從數(shù)值的左側(cè)從右向左遍歷,選擇第一個(gè)比他小的數(shù)值的右側(cè)插入該數(shù)值,其他數(shù)值依次向后賦值
#插入排序
a =[7,1,3,2,6,54,4,4,5,8,12,34]
for i in range(0,len(a)-1):
temp=a[i+1]
j=i+1
while j>=0 and temp<a[j-1]:
j=j-1
print(j)
if j>=-1:
k= i+1
while k>=j:
a[k]=a[k-1]
k=k-1
print(a)
a[j]=temp
print(a)
插入排序方法2,用到了列表的a.insert(1,2)和清楚a[2:3]=[],這樣可以少用一個(gè)循環(huán)
a =[7,1,3,2,6,54,4,4,5,8,12,34]
for i in range(1,len(a)-1):
temp=a[i]
j=i-1
while j>=0 and temp<=a[j]:
print(temp)
j=j-1
if j >=-1:
a[i:i+1]=[]
a.insert(j+1,temp)
print(a)
print(a)
到此這篇關(guān)于Python實(shí)現(xiàn)排序的四種方法的文章就介紹到這了,更多相關(guān)python排序方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python計(jì)算波峰波谷值的方法(極值點(diǎn))
這篇文章主要介紹了python求極值點(diǎn)(波峰波谷)求極值點(diǎn)主要用到了scipy庫(kù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python turtle庫(kù)的畫筆控制說(shuō)明
這篇文章主要介紹了Python turtle庫(kù)的畫筆控制說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
對(duì)Tensorflow中的矩陣運(yùn)算函數(shù)詳解
今天小編就為大家分享一篇對(duì)Tensorflow中的矩陣運(yùn)算函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Tensorflow: 從checkpoint文件中讀取tensor方式
今天小編就為大家分享一篇Tensorflow: 從checkpoint文件中讀取tensor方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python中時(shí)間類型的JSON數(shù)據(jù)轉(zhuǎn)換
在Python中,處理時(shí)間和日期數(shù)據(jù)以及與JSON數(shù)據(jù)的相互轉(zhuǎn)換是常見的任務(wù),本文主要為大家詳細(xì)如何在Python中處理時(shí)間類型的JSON數(shù)據(jù)轉(zhuǎn)換,需要的小伙伴可以參考下2024-02-02
python快速建立超簡(jiǎn)單的web服務(wù)器的實(shí)現(xiàn)方法
某些條件測(cè)試,需要一個(gè)簡(jiǎn)單的web服務(wù)器測(cè)試一下,為此專門去配置個(gè)nginx 或者 apache服務(wù)器略顯麻煩,這里就為大家介紹一下使用python快速建立超簡(jiǎn)單的web服務(wù)器的方法,需要的朋友可以參考下2018-02-02
詳解Python連接MySQL數(shù)據(jù)庫(kù)的多種方式
這篇文章主要介紹了Python連接MySQL數(shù)據(jù)庫(kù)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

