冒泡排序:
def bubble_sort(alist):
n = len(alist)
count = 0
for i in range(n-1):
for j in range(n-1-i): # 为排好序的元素个数
if alist[j] > alist[j + 1]:
alist[j], alist[j+1] = alist[j+1], alist[j]
count += 1
if count == 0:
break
print(count)
if __name__ == '__main__':
alist = [3,4,6,8,9]
bubble_sort(alist)
print(alist)
选择排序:
'''
选择排序:时间复杂度 O(n^2)
列表: list = [2,4,7,7,5,1]
查找指定元素的下标
index = list.index(4)
删除指定元素
list.remove(4)
删除指定下标
del list[0]
'''
def selection_sort(alist):
n = len(alist)
# n-1 是因为最后一个数不用排序
for i in range(n-1):
min_index = i
for j in range(i+1, n):
if alist[j] < alist[min_index]:
min_index = j
if min_index != i:
alist[i], alist[min_index] = alist[min_index], alist[i]
if __name__ == '__main__':
list = [2, 4, 7, 7, 5, 1, 8, 3]
selection_sort(list)
print(list)
快速排序:
'''
快速排序
最优时间复杂度:O(nlogn)
最坏时间复杂度:O(n2)
'''
def quick_sort(arr):
if len(arr) <= 1:
return arr
mark = arr[0]
less = [i for i in arr[1:] if i <= mark]
greater = [i for i in arr[1:] if i > mark]
res = quick_sort(less) + [mark] + quick_sort(greater)
return res
# 拆分式
def quick_sort2(arr):
if len(arr) <= 1:
return arr
mark = arr[0]
less = []
greater = []
for i in arr:
if i <= mark:
less.append(i)
if i > mark:
greater.append(i)
res = quick_sort(less) + quick_sort(greater)
return res
if __name__ == '__main__':
alist = [5, 6, 2, 7, 3]
result = quick_sort2(alist)
print(result)
插入排序:
'''
插入排序:
最优时间复杂度:O(n)
最坏时间复杂度:O(n2)
'''
def insert_sort(alist):
n = len(alist)
for i in range(1, n):
# 用该元素和前面的元素比较, 如果比前一个小,交换位置
for j in range(i, 0, -1):
if alist[j] < alist[j-1]:
alist[j], alist[j-1] = alist[j-1], alist[j]
if __name__ == '__main__':
list = [2, 4, 7, 7, 5, 1, 8, 3]
insert_sort(list)
print(list)
归并排序:
#coding:utf-8
def merge_sort(alist):
# 列表拆分
n = len(alist)
mid = n // 2
if n == 1:
return alist
sorted_list = []
left_list = merge_sort(alist[:mid])
right_list = merge_sort(alist[mid:])
left, right = 0, 0
left_n = len(left_list)
right_n = len(right_list)
# 当索引分别小于左右两个列表长度的时候说明游标没有遍历完任何一个列表,此时不断重复对比
while left < left_n and right < right_n:
# 对比两个列表中的数值,小的放到返回列表中
if left_list[left] < right_list[right]:
sorted_list.append(left_list[left])
left += 1
else:
sorted_list.append(right_list[right])
right += 1
# 循环结束之后,有一个列表尚未遍历完毕,此时采用列表相加的方式将剩余数据添加到返回列表的尾部,剩余数据是有序的,将大于返回列表之前的所有数据
sorted_list += left_list[left:]
sorted_list += right_list[right:]
return sorted_list
if __name__ == '__main__':
alist = [12,34,56,78,90,98,76,54,32,21]
result = merge_sort(alist)
print(alist)
print(result)
二分查找:
'''
二分查找 仅当数列有序时才管用, 使用的最多步骤为log2n(2为底数)
时间复杂度 O(logn)
'''
def binary_search(list, item):
low = 0
high = len(list) - 1
while low <= high:
mid = (low + high)//2
guess = list[mid]
if guess == item:
return mid
if guess > item:
high = mid - 1
else:
low = mid + 1
return None
if __name__ == '__main__':
mylist = [1, 3, 5, 7, 9, 11]
index = binary_search(mylist, 13)
print(index)
二叉树:
性质1:在二叉树的第i层上至多有2^(i-1)个结点(i>0)
性质2:深度为k的二叉树至多有2^k - 1个结点(k>0)
性质3:对于任意一棵二叉树,如果其叶结点数为N0,而度数为2的结点总数为N2,则N0=N2+1;
性质4:具有n个结点的完全二叉树的深度必为 log2(n+1)
性质5:对完全二叉树,若从上至下、从左至右编号,则编号为i 的结点,其左孩子编号必为2i,其右孩子编号必为2i+1;其双亲的编号必为i/2(i=1 时为根,除外)
完全二叉树: 最多只有2个子节点
满二叉树: 特殊的完全二叉树,除最外层,每个节点都有2个节点
二叉树的遍历
广度遍历 -- 层级遍历
深度遍历:都是根据根节点决定
先序遍历:
中序遍历:
后序遍历:
# 创建二叉树
class Binary_tree(object):
def __init__(self):
self.root = None
def add(self, item):
new_Node = Node(item)
# 判断是否为空树
if not self.root:
self.root = new_Node
else:
# 创建节点保持队列
queue = []
queue.append(self.root)
# 遍历所有节点
while len(queue) > 0:
# 获取一个节点,判断其左右节点是否存在,如有缺失补充
node = queue.pop(0)
if node.lchild:
queue.append(node.lchild)
else:
node.lchild = new_Node
return
if node.rchild:
queue.append(node.rchild)
else:
node.rchild = new_Node
return
二叉树的遍历
# 广度优先遍历
def breadth_travel(self):
if not self.root:
return
else:
queue = []
queue.append(self.root)
while len(queue) > 0:
node = queue.pop(0)
print(node.item, end=' ')
if node.lchild:
queue.append(node.lchild)
if node.rchild:
queue.append(node.rchild)
# 深度优先遍历
# 先序遍历
def pre_travel(self, root):
if root:
print(root.item, end=" ")
self.pre_travel(root.lchild)
self.pre_travel(root.rchild)
# 中序遍历
def in_travel(self, root):
if root:
self.in_travel(root.lchild)
print(root.item, end=" ")
self.in_travel(root.rchild)
# 后序遍历
def after_travel(self, root):
if root:
self.after_travel(root.lchild)
self.after_travel(root.rchild)
print(root.item, end=" ")
二叉树测试:
if __name__ == '__main__':
bTree = Binary_tree()
bTree.add(0)
bTree.add(1)
bTree.add(2)
bTree.add(3)
bTree.add(4)
bTree.add(5)
bTree.add(6)
bTree.add(7)
bTree.add(8)
bTree.add(9)
bTree.breadth_travel()
print()
bTree.pre_travel(bTree.root)
print()
bTree.in_travel(bTree.root)
print()
bTree.after_travel(bTree.root)