python基本算法

 

1.冒泡排序node

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.選擇排序ide

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.快速排序oop

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.二叉樹post

 

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
結果
相關文章
相關標籤/搜索