Python實現經典排序演算法

2021-09-11 07:43:43 字數 4453 閱讀 5226

import random

lis=

list

(range

(100))

random.

shuffle

(lis)

print

(lis)

def bubblesort

(arr)

:for i in

range(1

,len

(arr)):

for j in

range(0

,len

(arr)

-i):

if arr[j]

>arr[j+1]

: arr[j]

,arr[j+1]

=arr[j+1]

,arr[j]

return arr

print

(bubblesort

(lis)

)

def selectionsort

(arr)

:for i in

range

(len

(arr)):

temp=i

for j in

range

(i,len

(arr)-1

):if arr[temp]

>arr[j]

: temp=j

arr[i]

,arr[temp]

=arr[temp]

,arr[i]

return arr

print

(selectionsort

(lis)

)

def insertionsort

(arr)

:for i in

range(1

,len

(arr)):

for j in

range(0

,i):

if arr[i]

:for k in

range

(j,i)

: arr[k]

,arr[i]

=arr[i]

,arr[k]

return arr

print

(insertionsort

(lis)

)

import math

def shellsort

(arr)

: gap=

1while

(gap <

len(arr)/3

):gap = gap*3+

1while gap >0:

for i in

range

(gap,

len(arr)):

temp = arr[i]

j = i-gap

while j >=

0 and arr[j]

> temp:

arr[j+gap]

=arr[j]

j-=gap

arr[j+gap]

= temp

gap = math.

floor

(gap/3)

return arr

print

(shellsort

(lis)

)

#遞迴

def mergesort

(arr)

: n =

len(arr)

if n <=1:

return arr

n = n//2

sub1 =

mergesort

(arr[

:n])

sub2 =

mergesort

(arr[n:])

return

merge

(sub1, sub2)

#合併兩個有序陣列

def merge

(sub1, sub2)

: n1 =

len(sub1)

n2 =

len(sub2)

i =0 j =

0 merge =

while i < n1 and j < n2:

if sub1[i]

< sub2[j]

: merge.

(sub1[i]

) i +=

1else

: merge.

(sub2[j]

) j +=

1 # 沒複製完的

while i < n1:

merge.

(sub1[i]

) i +=

1while j < n2:

merge.

(sub2[j]

) j +=

1return merge

print

(mergesort

(lis)

)

def mergesort

(arr)

: n =

len(arr)

size =

1 # 表示每一步要歸併的陣列的長度

m =while size <= n:

for i in

range(0

, n-size, size+size)

: m =

merge

(arr[i: i+size]

, arr[i+size:

min(i+size+size, n)

]) # min

(i+size+size, n)為了最後一組陣列可能不夠全

arr[i:

min(i+size+size, n)

]= m[

:] # 把merge得到的結果賦值給原始陣列

size += size # 陣列長度每次乘二

return m

#合併兩個有序陣列

def merge

(sub1, sub2)

: n1 =

len(sub1)

n2 =

len(sub2)

i =0 j =

0 merge =

while i < n1 and j < n2:

if sub1[i]

< sub2[j]

: merge.

(sub1[i]

) i +=

1else

: merge.

(sub2[j]

) j +=

1 # 沒複製完的

while i < n1:

merge.

(sub1[i]

) i +=

1while j < n2:

merge.

(sub2[j]

) j +=

1return merge

print

(mergesort

(lis)

)

def quicksort

(arr)

:return

helpqs

(arr,0,

len(arr)-1

)def helpqs

(arr,left,right)

:if left>right:

return

k=arr[left]

i=left

j=right

while i!=j:

while arr[j]

>k and ij-=

1while arr[i]

<=k and ii+=

1if iarr[i]

,arr[j]

=arr[j]

,arr[i]

arr[left]

,arr[i]

=arr[i]

,arr[left]

helpqs

(arr,left,i-1)

helpqs

(arr,i+

1,right)

return arr

print

(quicksort

(lis)

)

Python 實現經典排序演算法

穩定的排序演算法 氣泡排序 插入排序 歸併排序和基數排序。不是穩定的排序演算法 選擇排序 快速排序 希爾排序 堆排序。a 80,15,45,35,88,46,55,66,22,99,0,1,100 for j in range 0,len a for i in range 0,len a 1 if ...

Python實現經典排序演算法 堆排序

上次說到了經典演算法選擇排序,感覺是比較簡單的演算法,這一次說一說稍微有點難度的堆排序。堆排序的時間複雜度要明顯優於前面的氣泡排序,插入排序和選擇排序 侷限於n較大時 先來講講堆 二叉堆 是乙個陣列,它可以近似被看作是乙個完全二叉樹。樹上每乙個節點對應乙個元素,除了最底層外,該樹是完全充滿的,而且是...

Python實現經典排序演算法 氣泡排序

1.氣泡排序概述 氣泡排序是一種簡單的排序演算法。它重複地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重複地進行直到沒有再需要交換,也就是說該數列已經排序完成。這個演算法的名字由來是因為越小的元素會經由交換慢慢 浮 到數列的頂端。2.演算法思想 比較相鄰...