演算法 一攬子排序演算法(1)

2021-08-03 19:20:50 字數 4065 閱讀 8480

def

bubblesort

(lis):

for i in range(len(lis)):

print(lis)

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

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

lis[j-1],lis[j] = lis[j],lis[j-1]

"""[5, 4, 2, 1, 6, 3]

[4, 2, 1, 5, 3, 6]

[2, 1, 4, 3, 5, 6]

[1, 2, 3, 4, 5, 6]

[1, 2, 3, 4, 5, 6]

[1, 2, 3, 4, 5, 6]

"""

def

selectsort

(lis):

for i in reversed(range(len(lis))):

print(lis)

max_index = i

for j in range(i):

if lis[j] > lis[max_index]:

max_index = j

lis[max_index],lis[i] = lis[i],lis[max_index]

"""[5, 4, 2, 1, 6, 3]

[5, 4, 2, 1, 3, 6]

[3, 4, 2, 1, 5, 6]

[3, 1, 2, 4, 5, 6]

[2, 1, 3, 4, 5, 6]

[1, 2, 3, 4, 5, 6]

"""

def

insertionsort

(lis):

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

item = lis[i]

j = i - 1

while j >= 0

and lis[j] > item:

lis[j+1] = lis[j]

j = j - 1

lis[j+1] = item

print(lis)

"""[4, 5, 2, 1, 7, 6, 3]

[2, 4, 5, 1, 7, 6, 3]

[1, 2, 4, 5, 7, 6, 3]

[1, 2, 4, 5, 7, 6, 3]

[1, 2, 4, 5, 6, 7, 3]

[1, 2, 3, 4, 5, 6, 7]

"""

def

shellsort

(lis):

h,n = 1,len(lis)

while h < n/3 :

h = 3*h + 1

#1,4,13,40...

while h>= 1:

for i in range(h,n):

j = i

while j >= h and lis[j] < lis[j-h]:

lis[j],lis[j-h] = lis[j-h],lis[j]

j -= h

print(lis)

h = h // 3

"""[5, 4, 8, 2, 1, 7, 6, 3] //原列表

[1, 4, 6, 2, 5, 7, 8, 3]

[1, 2, 3, 4, 5, 6, 7, 8]

"""

[1, 4, 6, 2, 5, 7, 8, 3]為4有序,即1,5||4,7||6,8||2,3這些中間間隔三位的數字都是有序的。然後h//3=>1變為1有序,可以大大減少選擇排序比較的次數。

class

mergesort:

defmergesort

(self,lis):

if len(lis) <= 1:

return lis

mid = len(lis) // 2

left = self.mergesort(lis[:mid])

right = self.mergesort(lis[mid:])

print("left:,right:".format(left,right))

return self.__mergetolist(left,right)

def__mergetolist

(self,left,right):

lis =

l,r = 0,0

while l < len(left) and r < len(right):

if left[l] < right[r]:

l += 1

else:

r += 1

lis += left[l:]

lis += right[r:]

return lis

"""[5, 4, 8, 2, 1, 7, 6, 3]

left:[5],right:[4]

left:[8],right:[2]

left:[4, 5],right:[2, 8]

left:[1],right:[7]

left:[6],right:[3]

left:[1, 7],right:[3, 6]

left:[2, 4, 5, 8],right:[1, 3, 6, 7]

[1, 2, 3, 4, 5, 6, 7, 8]

"""

class

quicksort

():def

sort

(self,lis):

self.__sort(lis,0,len(lis)-1)

def__sort

(self,lis,start,end):

if end <= start:

return

j = self.__partition(lis,start,end)

self.__sort(lis,start,j-1)

self.__sort(lis,j+1,end)

def__partition

(self,lis,start,end):

i,j = start+1,end

pivot = lis[start]

while

true: #這個迴圈找出(左》pivot)和(右while lis[i] < pivot:

if i == end:

break

i += 1

while lis[j] > pivot:

if j == start:

break

j -= 1

if i>=j:

break

lis[i],lis[j] = lis[j],lis[i]

lis[start],lis[j] = lis[j],lis[start]#把哨兵放到正確的位置

return j

三向劃分的改進版本->用於有大量重複元素
class

quick3way:

defsort

(self,lis):

self.__sort(lis,0,len(lis)-1)

def__sort

(self,lis,start,end):

if end <= start:

return

lt,i,gt = start,start+1,end

pivot = lis[start]

while i <= gt:

if lis[i] < pivot:

lis[lt],lis[i] = lis[i],lis[lt]

i += 1

lt += 1

elif lis[i] > pivot:

lis[i],lis[gt] = lis[gt],lis[i]

gt -= 1

else:

i += 1

self.__sort(lis,start,lt-1)

self.__sort(lis,gt+1,end)

Oracle EBS SQL 失效一攬子PO協議

1.查詢一攬子po 這裡要注意的是 不同的付款條件,幣種,稅率,都會可能產生一張新的一攬子po協議 複製內容到剪貼簿 程式 select pla.po line id,pla.po header id,pla.item id,msib.segment1 item code,pla.unit pric...

Redis一攬子命令 未完待續

1.del,刪除乙個或多個key,返回數量 set name hs del name set name k set color y del name color 刪除多個 2.exists,檢查乙個key是否存在 set db redis exists db 3.expire,為key設定生存時間 ...

linux系統安裝mysql一攬子報錯解決

解決方法 在終端輸入mysql uroot p123456登入資料庫總是報這樣的錯,全域性搜尋mysql.sock這個檔案,發現在 var lib mysql mysql.sock,於是建立乙個軟連線 ln s var lib mysql mysql.sock tmp mysql.sock 在終端輸...