1.冒泡排序
data_set = [ 9,1,22,31,45,3,6,2,11 ] loop_count = 0 for j in range(len(data_set)): for i in range(len(data_set) - j- 1): # -1 是因为每次比对的都 是i 与i +1,不减1的话,最后一次对比会超出list 获取范围,-j是因为,每一次大loop就代表排序好了一个最大值,放在了列表最后面,下次loop就不用再运算已经排序好了的值 了 if data_set[i] > data_set[i+1]: #switch tmp = data_set[i] data_set[i] = data_set[i+1] data_set[i+1] = tmp loop_count +=1 print(data_set) print(data_set) print("loop times", loop_count)
[1, 9, 22, 31, 3, 6, 2, 11, 45] [1, 9, 22, 3, 6, 2, 11, 31, 45] [1, 9, 3, 6, 2, 11, 22, 31, 45] [1, 3, 6, 2, 9, 11, 22, 31, 45] [1, 3, 2, 6, 9, 11, 22, 31, 45] [1, 2, 3, 6, 9, 11, 22, 31, 45] [1, 2, 3, 6, 9, 11, 22, 31, 45] [1, 2, 3, 6, 9, 11, 22, 31, 45] [1, 2, 3, 6, 9, 11, 22, 31, 45] [1, 2, 3, 6, 9, 11, 22, 31, 45] loop times: 36
2.选择排序
data_set = [ 9,1,22,31,45,3,6,2,11 ] smallest_num_index = 0 #初始列表最小值,默认为第一个 loop_count = 0 for j in range(len(data_set)): for i in range(j,len(data_set)): if data_set[i] < data_set[smallest_num_index]: #当前值 比之前选出来的最小值 还要小,那就把它换成最小值 smallest_num_index = i loop_count +=1 else: print("smallest num is ",data_set[smallest_num_index]) tmp = data_set[smallest_num_index] data_set[smallest_num_index] = data_set[j] data_set[j] = tmp print(data_set) print("loop times", loop_count)
('smallest num is ', 1) [1, 9, 22, 31, 45, 3, 6, 2, 11] ('loop times', 9) ('smallest num is ', 2) [1, 2, 22, 31, 45, 3, 6, 9, 11] ('loop times', 17) ('smallest num is ', 3) [1, 2, 3, 31, 45, 22, 6, 9, 11] ('loop times', 24) ('smallest num is ', 6) [1, 2, 3, 6, 45, 22, 31, 9, 11] ('loop times', 30) ('smallest num is ', 9) [1, 2, 3, 6, 9, 22, 31, 45, 11] ('loop times', 35) ('smallest num is ', 11) [1, 2, 3, 6, 9, 11, 31, 45, 22] ('loop times', 39) ('smallest num is ', 22) [1, 2, 3, 6, 9, 11, 22, 45, 31] ('loop times', 42) ('smallest num is ', 31) [1, 2, 3, 6, 9, 11, 22, 31, 45] ('loop times', 44) ('smallest num is ', 45) [1, 2, 3, 6, 9, 11, 22, 31, 45] ('loop times', 45)
3.快速排序
def quick_sort(array, left, right): ''' :param array: :param left: 列表的第一个索引 :param right: 列表最后一个元素的索引 :return: ''' if left >= right: return low = left high = right key = array[low] # 第一个值 sum=0 while low < high: # 只要左右未遇见 while low < high and array[high] > key: # 找到列表右边比key大的值 为止 high -= 1 sum+=1 print array[high] print "high第一次:",high print "sum:",sum # 此时直接 把key(array[low]) 跟 比它大的array[high]进行交换 array[low] = array[high] array[high] = key while low < high and array[low] <= key: # 找到key左边比key大的值,这里为何是<=而不是<呢?你要思考。。。 low += 1 # array[low] = print "low:",low # 找到了左边比k大的值 ,把array[high](此时应该刚存成了key) 跟这个比key大的array[low]进行调换 array[high] = array[low] array[low] = key print "high:",high print array quick_sort(array, left, low - 1) # 最后用同样的方式对分出来的左边的小组进行同上的做法 quick_sort(array, low + 1, right) # 用同样的方式对分出来的右边的小组进行同上的做法 print "==========================" if __name__ == '__main__': # array = [96, 14, 10, 9, 6, 99, 16, 5, 1, 3, 2, 4, 1, 13, 26, 18, 2, 45, 34, 23, 1, 7, 3, 22, 19, 2] # array = [8,4,1, 14, 6, 2, 3, 9,5, 13, 7,1, 8,10, 12] array=[6,2,7,3,8,9] print("before sort:", array) quick_sort(array, 0, len(array) - 1) print("-------final -------") print(array)
('before sort:', [6, 2, 7, 3, 8, 9]) 8 3 high第一次: 3 sum: 2 low: 2 high: 3 [3, 2, 6, 7, 8, 9] 6 high第一次: 2 sum: 3 low: 2 high: 2 [3, 2, 6, 7, 8, 9] high第一次: 1 sum: 0 low: 1 high: 1 [2, 3, 6, 7, 8, 9] ========================== 8 7 high第一次: 3 sum: 2 low: 3 high: 3 [2, 3, 6, 7, 8, 9] 8 high第一次: 4 sum: 1 low: 4 high: 4 [2, 3, 6, 7, 8, 9] ========================== ========================== ========================== -------final ------- [2, 3, 6, 7, 8, 9]
4.二叉树
class TreeNode(object): def __init__(self, data=0, left=0, right=0): self.data = data self.left = left self.right = right class BTree(object): def __init__(self, root=0): self.root = root def preOrder(self, treenode): if treenode is 0: return print(treenode.data) self.preOrder(treenode.left) self.preOrder(treenode.right) def inOrder(self, treenode): if treenode is 0: return self.inOrder(treenode.left) print(treenode.data) self.inOrder(treenode.right) def postOrder(self, treenode): if treenode is 0: return self.postOrder(treenode.left) self.postOrder(treenode.right) print(treenode.data) if __name__ == '__main__': n1 = TreeNode(data=1) n2 = TreeNode(2, n1, 0) n3 = TreeNode(3) n4 = TreeNode(4) n5 = TreeNode(5, n3, n4) n6 = TreeNode(6, n2, n5) n7 = TreeNode(7, n6, 0) n8 = TreeNode(8) root = TreeNode('root', n7, n8) bt = BTree(root) print("preOrder".center(50, '-')) print(bt.preOrder(bt.root)) print("inOrder".center(50, '-')) print (bt.inOrder(bt.root)) print("postOrder".center(50, '-')) print (bt.postOrder(bt.root))
---------------------preOrder--------------------- root 7 6 2 1 5 3 4 8 None ---------------------inOrder---------------------- 1 2 6 3 5 4 7 root 8 None --------------------postOrder--------------------- 1 2 3 4 5 6 7 8 root None