十大排序演算法的Python實現

2021-06-26 12:23:04 字數 3820 閱讀 2260

__author__ = 'qiao'

# 十大排序演算法的python實現

# 1、氣泡排序

def bubble(iarray):

length = len(iarray)

for i in range(length):

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

if iarray[j] > iarray[j + 1]:

tmp = iarray[j]

iarray[j] = iarray[j + 1]

iarray[j + 1] = tmp

# 2、選擇排序

def select(iarray):

length = len(iarray)

for i in range(length):

index = 0

imax = iarray[index]

for j in range(length - i):

if iarray[j] > imax:

imax = iarray[j]

index = j

if index != length - i - 1:

tmp = iarray[index]

iarray[index] = iarray[length - i - 1]

iarray[length - i - 1] = tmp

# 3、插入排序

def insert(iarray):

length = len(iarray)

for i in range(1, length):

j = i

target = iarray[j]

while j > 0 and target < iarray[j - 1]:

iarray[j] = iarray[j - 1]

j -= 1

iarray[j] = target

# 4、希爾排序

def shell(iarray):

length = len(iarray)

gap = length // 2

while gap:

for i in range(gap):

for j in range(i + gap, length, gap):

target = iarray[j]

while j > i and target < iarray[j - gap]:

iarray[j] = iarray[j - gap]

j -= gap

iarray[j] = target

gap //= 2

# 5、歸併排序

# 合併兩有序陣列

def mergearray(iarray, first, mid, last, tmp=list()):

i, j = first, mid + 1

m, n = mid, last

k = 0

while i <= m and j <= n:

if iarray[i] <= iarray[j]:

tmp.insert(k, iarray[i])

i += 1

else:

tmp.insert(k, iarray[j])

j += 1

k += 1

while i <= m:

tmp.insert(k, iarray[i])

i += 1

k += 1

while j <= n:

tmp.insert(k, iarray[j])

j += 1

k += 1

for i in range(k):

iarray[first + i] = tmp[i]

def merge(iarray, first=0, last=7, tmp=list()):

if first < last:

mid = (first + last) // 2

merge(iarray, first, mid, tmp)

merge(iarray, mid + 1, last, tmp)

mergearray(iarray, first, mid, last, tmp)

# 6、堆排序

def heap(iarray):

pass

# 7、快速排序

def quick(iarray):

pass

# 8、計數排序

def count(iarray):

pass

# 9、基數排序

def base(iarray):

pass

# 10、桶排序

def bucket(iarray):

pass

# 約瑟夫環n = 10, m = 3

def josephuf(n, k):

link = [x for x in range(1, n + 1)]

index = 0

for loop_i in range(n - 1):

index = (index + k) % len(link)

index -= 1

print('kill:', link[index])

del link[index]

if index == -1: # the last element of link

index = 0

print('survive :', link[0])

def choose(num, iarray):

if num == 1:

print("氣泡排序:")

bubble(iarray)

elif num == 2:

print("選擇排序:")

select(iarray)

elif num == 3:

print("插入排序:")

insert(iarray)

elif num == 4:

print("希爾排序:")

shell(iarray)

elif num == 5:

print("歸併排序:")

merge(iarray)

elif num == 6:

print("堆排序:")

heap(iarray)

elif num == 7:

print("快速排序:")

quick(iarray)

elif num == 8:

print("計數排序:")

count(iarray)

elif num == 9:

print("基數排序:")

base(iarray)

else:

print("桶排序:")

bucket(iarray)

if __name__ == '__main__':

# array = [2, 1, 3, 5, 2, 7, 9, 8] # len = 8

# print("the length of the array:", len(array))

# print(array)

# _i = input("請選擇:")

# _i = 5

# while int(_i):

# choose(int(_i), array)

# print(array)

# _i = input("請選擇:")

josephuf(10, 3)

python實現十大排序演算法

平方階o n 2 插入排序 選擇排序 氣泡排序 線性對數階o nlogn 快速排序 堆排序 歸併排序 o n 1 a a介於0到1之間 希爾排序 線性階o n 基數排序 桶排序 計數排序 穩定 氣泡排序 插入排序 歸併排序 基數排序 不穩定 選擇排序 快速排序 希爾排序 堆排序 in place,不...

十大排序演算法python實現

冒泡改進 有可能迭代到中間就已經全部有序了。如果一次遍歷元素都沒交換位置說明已經有序了,停止迭代。這裡引進了乙個計數。def bubble sort l for i in range len l change 0 for j in range len l i 1 if l j l j 1 l j l...

十大排序演算法以及python實現

排序演算法可以說是非常重要的基礎演算法。可能一般少有單獨排序的任務,但是大多任務都是基於排序演算法的。現在,對於排序演算法進行乙個總結。排序演算法可以分為兩種,第一種是比較類的,第二種是非比較類的。比較類演算法包括插入排序,選擇排序,快速排序等等方法 非比較類演算法包括計數排序,桶排序和計數排序。比...