代码均好使,放心使用。
冒泡排序
方法一
a = [3,5,4,9,10,2]
count = 0
for i in range(len(a)-1):
for j in range(len(a)-1-i):
if a[j] >a[j+1]:
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
del temp
count += 1
print(a)
print(count)
方法二
a = [1,5,6,70,9,0]
for i in range(len(a)-1):
for j in range(len(a)-i-1):
if a[j] > a[j+1]:
a[j],a[j+1] = a[j+1],a[j]
print(a)
选择排序
方法一
def select_sort(slist):
for i in range(len(slist)):
x = i
for j in range(i, len(slist)):
if slist[j] < slist[x]:
x = j
slist[i], slist[x] = slist[x], slist[i]
return slist
slist = select_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(slist)
a = [5,8,10,6,7,9,1]
count = 0
for i in range(0,len(a)-1):
min_index = i
for j in range (i+1,len(a)):
if a[min_index] > a[j]:
min_index = j
if min_index != i :
temp = a[i]
a[i] = a[min_index]
a[min_index] = temp
count += 1
print(a)
print(count)
插入排序
def insert_sort(ilist):
for i in range(len(ilist)):
for j in range(i):
if ilist[i] < ilist[j]:
ilist.insert(j, ilist.pop(i))
break
return ilist
ilist = insert_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(ilist)
希尔排序
def shell_sort(slist):
gap = len(slist)
while gap > 1:
gap = gap // 2
for i in range(gap, len(slist)):
for j in range(i % gap, i, gap):
if slist[i] < slist[j]:
slist[i], slist[j] = slist[j], slist[i]
return slist
slist = shell_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(slist)
归并排序
def merge_sort(array):
def merge_arr(arr_l, arr_r):
array = []
while len(arr_l) and len(arr_r):
if arr_l[0] <= arr_r[0]:
array.append(arr_l.pop(0))
elif arr_l[0] > arr_r[0]:
array.append(arr_r.pop(0))
if len(arr_l) != 0:
array += arr_l
elif len(arr_r) != 0:
array += arr_r
return array
def recursive(array):
if len(array) == 1:
return array
mid = len(array) // 2
arr_l = recursive(array[:mid])
arr_r = recursive(array[mid:])
return merge_arr(arr_l, arr_r)
return recursive(array)
slist = merge_sort([4, 5, 6,0, 7, 3, 2, 6, 9, 8])
print(slist)
快速排序
方法一:
def quick_sort(qlist):
if qlist == []:
return []
else:
qfirst = qlist[0]
qless = quick_sort([l for l in qlist[1:] if l < qfirst])
qmore = quick_sort([m for m in qlist[1:] if m >= qfirst])
return qless + [qfirst] + qmore
qlist = quick_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(qlist)
方法二
def getMiddle(list,low,high): #定义一个中值方法;定义三个值,中值,小值,大值
tmp = list[low]
while low < high:
while low < high and tmp <= list[high]:
high -= 1
list[low] = list[high]
while low <high and tmp >= list[low]:
low += 1
list[high] = list[low]
list[low] = tmp
return low
def quit_sort(list,low,high):
if low < high:
m = getMiddle(list,low,high)
quit_sort(list,low,m-1)
quit_sort(list,m+1,high)
a = [8,7,3,0,1,0,8,9,6]
quit_sort(a,0,len(a)-1)
print(a)
堆排序
import copy
def heap_sort(hlist):
def heap_adjust(parent):
child = 2 * parent + 1 # left child
while child < len(heap):
if child + 1 < len(heap):
if heap[child + 1] > heap[child]:
child += 1 # right child
if heap[parent] >= heap[child]:
break
heap[parent], heap[child] = heap[child], heap[parent]
parent, child = child, 2 * child + 1
heap, hlist = copy.copy(hlist), []
for i in range(len(heap) // 2, -1, -1):
heap_adjust(i)
while len(heap) != 0:
heap[0], heap[-1] = heap[-1], heap[0]
hlist.insert(0, heap.pop())
heap_adjust(0)
return hlist
hlist = heap_sort([4, 5, 6, 7, 3, 2, 6, 9, 8])
print(hlist)
基数排序
def radix_sort(array):
bucket, digit = [[]], 0
while len(bucket[0]) != len(array):
bucket = [[], [], [], [], [], [], [], [], [], []]
for i in range(len(array)):
num = (array[i] // 10 ** digit) % 10
bucket[num].append(array[i])
array.clear()
for i in range(len(bucket)):
array += bucket[i]
digit += 1
return array
arry = radix_sort([1,0,3,10,15,100,112,311,200,60,4,5,311])
print(arry)