冒泡排序
__author__ = 'hp'
def bubble_sort(li):
"冒泡排序"
n = len(li)
for i in range(n-1):
for j in range(n-1-i):
if li[j]>li[j+1]:
li[j],li[j+1] = li[j+1],li[j]
def bubble_sort1(li):
"冒泡排序"
n = len(li)
for i in range(n-1,0,-1):
for j in range(i):
if li[j]>li[j+1]:
li[j],li[j+1] = li[j+1],li[j]
# 优化
def bubble_sort2(li):
n = len(li)
for i in range(n-1):
count = 0
for j in range(n-1-i):
if li[j]>li[j+1]:
li[j],li[j+1] = li[j+1],li[j]
count += 1
# 如果已经有序,退出循环
if count == 0:
return
li = [4,3,2,1]
bubble_sort1(li)
print(li)
选择排序
__author__ = 'hp'
# 选择排序
def select_sort(li):
n = len(li)
for j in range(n-1):
min_index = j #最小值位置
for i in range(j+1,n): # i为右侧无序遍历
if li[i] < li[min_index]:
min_index = i
if min_index != j:
li[min_index],li[j] = li[j],li[min_index]
li = [4,3,2,2,6,1]
select_sort(li)
print(li)
插入排序
__author__ = 'hp'
# 从右边无序第一个插入左边的有序中
def insert_sort(li):
n = len(li)
for j in range(1,n):
# 无序从位置j开始
for i in range(j,0,-1):
if li[i]< li[i-1]:
li[i],li[i-1] = li[i-1],li[i]
else:
break
li = [4,3,8,4,5,9,11,2]
insert_sort(li)
print(li)
快速排序
def quick_sort(li,left,right):
if left<right: # 至少要有两个元素
mid = partition(li,left,right)
quick_sort(li,left,mid-1)
quick_sort(li,mid+1,right)
# 排出一个中间值,左边都比它小,右边都比它大
def partition(li, left, right):
tmp = li[left] # tmp = li[0]=2 第一位拿出来当作中间值,第一位空出
while left < right:
while left < right and li[right]>=tmp: # 从右边找比tmp小的数
right -= 1 # 右边一直比tmp大,不停左移动,直到碰到比tmp小的,退出循环
li[left] = li[right] # 碰到比tmp小的数,扔回去左边的空位
while left <right and li[left] <=tmp:
left += 1
li[right] = li[left]
li[left] = tmp # 退出循环,left=right,放回tmp
return left # 返回tmp的位置
if __name__ == "__main__":
li = [2,3,1,2,3,4]
quick_sort(li,0,5)
print(li)
归并排序
# __author__ = 'hp'
#
# def merge(li,low,mid,high):
# tmp = []
# i = low
# j = mid+1
# while i<=mid and j<=high:
# if li[i]<li[j]:
# tmp.append(li[i])
# i += 1
# else:
# tmp.append(li[j])
# j += 1
# while i<=mid:
# tmp.append(li[i])
# i += 1
# while j<=high:
# tmp.append(li[j])
# j += 1
# li[low:high+1] = tmp
#
#
#
#
# def merge_sort(li,low,high):
# if low < high:
# mid = (low+high)//2
# merge_sort(li,low,mid)
# merge_sort(li,mid+1,high)
# merge(li,low,mid,high)
#
#
# if __name__ == "__main__":
# l1 = list(range(10))
# import random
# random.shuffle(l1)
# merge_sort(l1,0,len(l1)-1)
# print(l1)
# merge排序
def merge(li,left,mid,right):
i = left
j = mid+1
tmp =[]
while i<=mid and j<=right:
if li[i]<=li[j]:
tmp.append(li[i])
i += 1
else:
tmp.append(li[j])
j += 1
while i<=mid:
tmp.append(li[i])
i += 1
while j<=right:
tmp.append(li[j])
j += 1
li[left:right+1] = tmp
def merge_sort(li,left,right):
if left<right:
mid = (left+right)//2
merge_sort(li,left,mid)
merge_sort(li,mid+1,right)
merge(li,left,mid,right)
li = [1,3,2,4,7,5]
merge_sort(li,0,len(li)-1)
print(li)
堆排序
# 调整
def sift(li,low,high):
i = low
j = 2 * i +1
tmp = li[low]
while j<=high:
if j+1<=high and li[j+1]>li[j]:
j += 1
if li[j]>tmp:
li[i] = li[j]
i = j
j = 2 * i + 1
else:
li[i] = tmp
break
else:
li[i] = tmp
def heap_sort(li):
n = len(li)
for i in range((n-2)//2,-1,-1):
sift(li,i,n-1)
for i in range(n-1,0,-1):
li[i],li[0] = li[0],li[i]
sift(li,0,i-1)
li = [1,2,4,5,9,7,1,2,3,3,2,1]
heap_sort(li)
print(li)
再次堆排序
# 左右子树都是堆,自己不是堆,进行调整
def sift(li, low, high):
tmp = li[low]
i = low
j = 2*i + 1 # 左孩子
while j<=high:
if j+1<=high and li[j+1]>li[j]: #右孩子更大
j += 1
if li[j] > tmp:
li[i] = li[j]
i = j
j = 2*i + 1
else: #tmp对于孩子,可以退出了
li[i] = tmp
break
else:
li[i] = tmp
# 堆排序函数
def heap_sort(li):
# 1.建堆,从最后一个有孩子的节点
n = len(li)
for i in range((n-1-1)//2,-1,-1): # i表示根下标
sift(li,i,n-1) # 建堆完成
# 2.挨个出数
for i in range(n-1,-1,-1): # i 表示当前的high
li[i], li[0] = li[0], li[i] # li[0]是出数
#出数后堆得范围(0,i-1)
sift(li,0,i-1)
# li = [3,4,1,2,5,7]
# heap_sort(li)
# print(li)
# topK问题
def topk(li,k):
#1.建堆
heap = li[:k]
n = len(heap)
for i in range((n-2)//2,-1,-1):
sift(heap,0,n-1) # 调整生成大根堆
#2.比较
for i in range(k,len(li)):
if li[i]<heap[0]: # 比堆顶小的才放入堆里,最后生成的是最小topk
heap[0] = li[i]
sift(heap, 0, n-1)
#3. 出数
for i in range(n-1,-1,-1):
heap[i], heap[0] = heap[0], heap[i]
sift(heap,0,i-1)
return heap
li = [10,8,2,3,7,9,0,6,5,4]
print(topk(li,3))
print(li)
# 内置堆模块 heapq
# import heapq
# li = [1,3,7,2,4,9,0]
# heapq.heapify(li)
# print(li)
# r = [heapq.heappop(li) for i in range(len(li))]
# print(r)
基于递归的堆排序
# 递归实现堆排序
# 1 大根堆调整函数
def max_heapify(heap, heap_size, parent):
left = 2*parent + 1
right = 2*parent + 2
larger = parent
if left < heap_size and heap[left] > heap[larger]:
larger = left
if right < heap_size and heap[right] > heap[larger]:
larger = right
if larger != parent:
heap[parent],heap[larger] = heap[larger], heap[parent]
max_heapify(heap, heap_size, larger)
# 创建大根堆
def build_max_heap(heap):
n = len(heap)
lastparent = (n-2)//2
for i in range(lastparent, -1, -1):
max_heapify(heap, n, i)
# 堆排序
def heap_sort(heap):
build_max_heap(heap)
n = len(heap)
for i in range(n-1,-1,-1):
heap[0], heap[i] = heap[i], heap[0]
max_heapify(heap,i,0)