最简单的排序有三种:插入排序,选择排序和冒泡排序。这三种排序比较简单,它们的平均时间复杂度均为O(n^2),在这里对原理就不加赘述了。贴出来源代码。
插入排序:
def insertion_sort(sort_list):
iter_len = len(sort_list)
if iter_len < 2:
return sort_list
for i in range(1, iter_len):
key = sort_list[i]
j = i - 1
while j >= 0 and sort_list[j] > key:
sort_list[j+1] = sort_list[j]
j -= 1
sort_list[j+1] = key
return sort_list
冒泡排序:
def bubble_sort(sort_list):
iter_len = len(sort_list)
if iter_len < 2:
return sort_list
for i in range(iter_len-1):
for j in range(iter_len-i-1):
if sort_list[j] > sort_list[j+1]:
sort_list[j], sort_list[j+1] = sort_list[j+1], sort_list[j]
return sort_list
选择排序:
def selection_sort(sort_list):
iter_len = len(sort_list)
if iter_len < 2:
return sort_list
for i in range(iter_len-1):
smallest = sort_list[i]
location = i
for j in range(i, iter_len):
if sort_list[j] < smallest:
smallest = sort_list[j]
location = j
if i != location:
sort_list[i], sort_list[location] = sort_list[location], sort_list[i]
return sort_list
平均时间复杂度为O(nlogn)的算法有:归并排序,堆排序和快速排序。
#快排
def quickSort(lists):
if len(lists)<=1:
return lists
pivot = lists.pop()
left = []
right = []
for i in lists:
if i<pivot:
left.append(i)
else:
right.append(i)
return quickSort(left)+[pivot]+quickSort(right)
#归并排序
def mergesort(lists):
if len(lists)<=1:
return lists
mid = int(len(lists)/2)
left = lists[:mid]
right = lists[mid:]
mergesort(left)
mergesort(right)
return merge(left,right)
def merge(left,right):
i,j = 0,0
result = []
while i<len(left) and j<len(right):
if left[i]<=right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
j+=1
while(i<len(left)):
result.append(left[i])
i+=1
while(j<len(right)):
result.append(right[j])
j+=1
return result
#排序算法集合
import random
#冒泡排序
def BubbleSort(lists):
for i in range(0,len(lists)):
for j in range(0,len(lists)-1-i):
if lists[j]>lists[j+1]:
lists[j],lists[j+1] = lists[j+1],lists[j]
return lists
#选择排序
def selectionSort(lists):
for i in range(len(lists)):
min = i
for j in range(i+1,len(lists)):
if lists[j]<lists[min]:
min = j
if min != i:
lists[min],lists[i] = lists[i],lists[min]
return lists
#插入排序
def insertSort(lists):
for i in range(1,len(lists)):
key = lists[i]
j = i-1
while j>=0 and key<lists[j]:
lists[j+1] = lists[j]
j -= 1
lists[j+1]=key return lists
#希尔排序
def shellSort(lists):
count = len(lists)
step = 2
group = int(count/step)
while group>0:
for i in range(group):
j = i+group
while j<count:
key = lists[j]
k = j-group
while k>=0:
if key<lists[k]:
lists[k+group]=lists[k]
lists[k] = key
k -= group
j+=group
group = int(group/2)
return lists
#快排
def quickSort(lists):
if len(lists)<=1:
return lists
pivot = lists.pop()
left = []
right = []
for i in lists:
if i<pivot:
left.append(i)
else:
right.append(i)
return quickSort(left)+[pivot]+quickSort(right)
#归并排序
def mergesort(lists):
if len(lists)<=1:
return lists
mid = int(len(lists)/2)
left = lists[:mid]
right = lists[mid:]
left = mergesort(left)
right = mergesort(right)
return merge(left,right)
def merge(left,right):
i,j = 0,0
result = []
while i<len(left) and j<len(right):
if left[i]<=right[j]:
result.append(left[i])
i+=1
else:
result.append(right[j])
j+=1
while(i<len(left)):
result.append(left[i])
i+=1
while(j<len(right)):
result.append(right[j])
j+=1
return result
#归并排序
def mergeSort(lists): mid = int(len(lists)/2)
left,right = lists[:mid],lists[mid:]
if len(left)>1:left = mergeSort(left)
if len(right)>1:right = mergeSort(right)
res = []
# while left and right:
# if left[-1]>=right[-1]:
# res.append(left.pop())
# else:
# res.append(right.pop())
# res.reverse()
#return (left or right) + res
while left and right:
if left[0]<=right[0]:
res.append(left.pop(0))
else:
res.append(right.pop(0))
return res+(left or right)
def adjust_heap(lists,i,size):
lchild = 2*i+1
rchild = 2*i+2
max = i
if i<size/2:
if lchild<size and lists[lchild]>lists[max]:
max = lchild
if rchild>size and lists[rchild]>lists[max]:
max = rchild
if max != i:
lists[max],lists[i] = lists[i], lists[max]
adjust_heap(lists,max,size)
def build_heap(lists,size):
for i in range(0,int(size/2))[::-1]:
adjust_heap(lists,i,size)
def heap_sort(lists):
size = len(lists)
build_heap(lists,size)
for i in range(0,size)[::-1]:
lists[0],lists[i] = lists[i],lists[0]
adjust_heap(lists,0,i)
return lists if __name__ =='__main__':
lists = [random.randint(1,20) for n in range(10)]
#print(lists)
#print(sorted(lists))
#print(lists)
#print(BubbleSort(lists))
#print(lists)
#print(selectionSort(lists))
#print(lists)
#print(insertSort(lists))
#print(lists)
#print(shellSort(lists))
#print(lists)
#print(mergesort(lists))
#print(lists)
#print(quickSort(lists))
#print(lists)
#print(mergeSort(lists))
print(lists)
print(heap_sort(lists))