diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b80908c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: 当前文件", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git "a/0.1.\347\256\227\346\263\225\346\225\210\347\216\207\350\241\241\351\207\217.py" "b/0.1.\347\256\227\346\263\225\346\225\210\347\216\207\350\241\241\351\207\217.py" index 63a8e0c..a6c3683 100644 --- "a/0.1.\347\256\227\346\263\225\346\225\210\347\216\207\350\241\241\351\207\217.py" +++ "b/0.1.\347\256\227\346\263\225\346\225\210\347\216\207\350\241\241\351\207\217.py" @@ -9,9 +9,9 @@ for b in range(1001): c = 1000 - a - b if a**2 + b**2 == c**2: - print("a:%d, b:%d, c:%d " %(a,b,c)) + print("a:%d, b:%d, c:%d " % (a, b, c)) end_time = time.time() -print("time:%f" %(end_time-start_time)) # 1.31 +print("time:%f" % (end_time-start_time)) # 1.31 print("*"*30) # 第二次尝试 @@ -20,8 +20,8 @@ for b in range(1001-a): c = 1000 - a - b if a**2 + b**2 == c**2: - print("a:%d, b:%d, c:%d " %(a,b,c)) + print("a:%d, b:%d, c:%d " % (a, b, c)) end_time = time.time() -print("time:%f" %(end_time-start_time)) # 0.69 +print("time:%f" % (end_time-start_time)) # 0.69 -# 时间复杂度: T(n) = O(n*n*(1+1)) = O(n*n) = O(n2) \ No newline at end of file +# 时间复杂度: T(n) = O(n*n*(1+1)) = O(n*n) = O(n2) diff --git "a/1.0.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.md" "b/1.0.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.md" index 97cc8ad..7f4ed5c 100644 --- "a/1.0.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.md" +++ "b/1.0.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.md" @@ -1,7 +1,10 @@ # Python内置类型性能分析 + ## timeit模块 + timeit模块可以用来测试一小段Python代码的执行速度。 -```py + +``` py class timeit.Timer(stmt='pass', setup='pass', timer=) ``` @@ -12,22 +15,30 @@ stmt参数是要测试的代码语句(statment); setup参数是运行代码时需要的设置; timer参数是一个定时器函数,与平台有关。 -``` + +``` timeit.Timer.timeit(number=1000000) ``` + Timer类中测试语句执行速度的对象方法。number参数是测试代码时的测试次数,默认为1000000次。方法返回执行代码的平均耗时,一个float类型的秒数。 # list内置操作的时间复杂度 + ![1.0](image/1.0.png) # dict内置操作的时间复杂度 + ![1.1](image/1.1.png) ## Python的内置数据结构 + Python给我们提供了很多现成的数据结构类型,这些系统自己定义好的,不需要我们自己去定义的数据结构叫做Python的内置数据结构,比如列表、元组、字典。 + ## Python的扩展数据结构 + 而有些数据组织方式,Python系统里面没有直接定义,需要我们自己去定义实现这些数据的组织方式,这些数据组织方式称之为Python的扩展数据结构,比如栈,队列等。 + ## 算法与数据结构的区别 数据结构只是静态的描述了数据元素之间的关系。 diff --git "a/1.1.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.py" "b/1.1.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.py" index 6a37166..76b5f09 100644 --- "a/1.1.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.py" +++ "b/1.1.Python\345\206\205\347\275\256\347\261\273\345\236\213\346\200\247\350\203\275\345\210\206\346\236\220.py" @@ -1,21 +1,26 @@ # _*_ conding: UTF-8 _*_ from timeit import Timer + def test1(): - l = [] + L = [] for i in range(1000): - l = l + [i] + L = L + [i] + def test2(): - l = [] + L = [] for i in range(1000): - l.append(i) + L.append(i) + def test3(): - l = [i for i in range(1000)] + L = [i for i in range(1000)] + def test4(): - l = list(range(1000)) + L = list(range(1000)) + t1 = Timer("test1()", "from __main__ import test1") print("concat ", t1.timeit(number=1000), "seconds") @@ -27,14 +32,14 @@ def test4(): print("comprehension ", t3.timeit(number=1000), "seconds") t4 = Timer("test4()", "from __main__ import test4") -print("list range ", t4.timeit(number=1000), "seconds") +print("List range ", t4.timeit(number=1000), "seconds") """结果: concat 1.421693535000486 seconds append 0.1010527379994528 seconds comprehension 0.036659903999861854 seconds -list range 0.018424673000481562 seconds +List range 0.018424673000481562 seconds """ print("*"*30) @@ -46,15 +51,15 @@ def test4(): pop_end = Timer("x.pop()", "from __main__ import x") print("pop_end ", pop_end.timeit(number=1000), "seconds") -list_append = Timer("x.append(1)", "from __main__ import x") -print("list_append(从后边插入) ", list_append.timeit(number=1000), "seconds") +List_append = Timer("x.append(1)", "from __main__ import x") +print("List_append(从后边插入) ", List_append.timeit(number=1000), "seconds") -list_insert = Timer("x.insert(0,1)", "from __main__ import x") -print("list_insert(从前边插入) ", list_insert.timeit(number=1000), "seconds") +List_insert = Timer("x.insert(0,1)", "from __main__ import x") +print("List_insert(从前边插入) ", List_insert.timeit(number=1000), "seconds") """ pop_zero 3.0994806850003442 seconds pop_end 0.0001244389995918027 seconds -list_append(从后边插入) 9.197799954563379e-05 seconds -list_insert(从前边插入) 3.44098534800014 seconds -""" \ No newline at end of file +List_append(从后边插入) 9.197799954563379e-05 seconds +List_insert(从前边插入) 3.44098534800014 seconds +""" diff --git "a/2.1\345\215\225\351\223\276\350\241\250.py" "b/2.1\345\215\225\351\223\276\350\241\250.py" index 8f6131c..4c3563f 100644 --- "a/2.1\345\215\225\351\223\276\350\241\250.py" +++ "b/2.1\345\215\225\351\223\276\350\241\250.py" @@ -6,14 +6,17 @@ class SingleNode(object): """ 单链表的节点 """ - def __init__(self,item): + + def __init__(self, item): # _item 存放数据元素 self.item = item # _next 是下一个节点的标示 self.next = None + class SingleLinkList(object): """单链表""" + def __init__(self): self._head = None @@ -41,7 +44,7 @@ def travel(self): cur = cur.next print("") - def add(self,item): + def add(self, item): """头部添加元素""" # 先创建一个保存item值的节点 node = SingleNode(item) @@ -85,7 +88,7 @@ def insert(self, pos, item): # 将插入位置的前一个节点的next指向新节点 pre.next = node - def remove(self,item): + def remove(self, item): """删除节点""" cur = self._head pre = None @@ -114,17 +117,17 @@ def search(self, item): cur = cur.next return False + if __name__ == "__main__": ll = SingleLinkList() ll.add(1) ll.add(2) # 头部添加 ll.append(3) # 尾部添加 - ll.insert(2,4) # 指定位置插入 + ll.insert(2, 4) # 指定位置插入 print("length:", ll.length()) ll.travel() # 遍历链表 - print(ll.search(3)) # 搜索链表是否存在3 - print(ll.search(5)) # 搜索链表是否存在5 + print(ll.search(3)) # 搜索链表是否存在3 + print(ll.search(5)) # 搜索链表是否存在5 ll.remove(5) # 删除链表中的item:1 - print("length:",ll.length()) + print("length:", ll.length()) ll.travel() - diff --git "a/2.2\345\215\225\351\241\271\345\276\252\347\216\257\351\223\276\350\241\250.py" "b/2.2\345\215\225\351\241\271\345\276\252\347\216\257\351\223\276\350\241\250.py" index de64018..194edb2 100644 --- "a/2.2\345\215\225\351\241\271\345\276\252\347\216\257\351\223\276\350\241\250.py" +++ "b/2.2\345\215\225\351\241\271\345\276\252\347\216\257\351\223\276\350\241\250.py" @@ -6,12 +6,14 @@ class Node(object): """ 单链表的节点 """ - def __init__(self,item): + + def __init__(self, item): # _item 存放数据元素 self.item = item # _next 是下一个节点的标示 self.next = None + class SinCycLinkedlist(object): """单项循环链表""" @@ -45,7 +47,7 @@ def travel(self): print(cur.item) print("") - def add(self,item): + def add(self, item): """头部添加节点""" node = Node(item) if self.is_empty(): @@ -144,6 +146,7 @@ def search(self, item): return True return False + if __name__ == "__main__": ll = SinCycLinkedlist() ll.add(1) @@ -157,9 +160,5 @@ def search(self, item): print(ll.search(3)) print(ll.search(7)) ll.remove(1) - print("length:",ll.lengeth()) + print("length:", ll.lengeth()) ll.travel() - - - - diff --git "a/2.3\345\217\214\345\220\221\351\223\276\350\241\250.py" "b/2.3\345\217\214\345\220\221\351\223\276\350\241\250.py" index b6bb41b..8cfc9da 100644 --- "a/2.3\345\217\214\345\220\221\351\223\276\350\241\250.py" +++ "b/2.3\345\217\214\345\220\221\351\223\276\350\241\250.py" @@ -3,13 +3,16 @@ class Node(object): """双向链表节点""" + def __init__(self, item): self.item = item self.next = None self.prev = None + class DLinkList(object): """双向链表""" + def __init__(self): self._head = None @@ -123,6 +126,7 @@ def remove(self, item): break cur = cur.next + if __name__ == "__main__": ll = DLinkList() ll.add(1) @@ -138,4 +142,3 @@ def remove(self, item): ll.remove(1) print("length:", ll.length()) ll.travel() - diff --git "a/3.\346\240\210.py" "b/3.\346\240\210.py" index 15e61ae..0f93504 100644 --- "a/3.\346\240\210.py" +++ "b/3.\346\240\210.py" @@ -5,8 +5,10 @@ 使用list实现,其中list的append就是栈的push;list的pop就是栈的pop;list的[-1]就是栈的peek """ + class Stack(object): """栈""" + def __init__(self): self.items = [] @@ -30,6 +32,7 @@ def size(self): """返回栈的大小""" return len(self.items) + if __name__ == "__main__": stack = Stack() stack.push("hello") diff --git "a/4.1\351\230\237\345\210\227.py" "b/4.1\351\230\237\345\210\227.py" index 24d070d..b33ae2a 100644 --- "a/4.1\351\230\237\345\210\227.py" +++ "b/4.1\351\230\237\345\210\227.py" @@ -6,8 +6,10 @@ 先进先出 """ + class Queue(object): """队列""" + def __init__(self): self.items = [] diff --git "a/4.2\345\217\214\347\253\257\351\230\237\345\210\227.py" "b/4.2\345\217\214\347\253\257\351\230\237\345\210\227.py" index 0de2f93..ba99629 100644 --- "a/4.2\345\217\214\347\253\257\351\230\237\345\210\227.py" +++ "b/4.2\345\217\214\347\253\257\351\230\237\345\210\227.py" @@ -4,8 +4,10 @@ 可以在任一端入队和出队 """ + class Deque(object): """双端队列""" + def __init__(self): self.items = [] @@ -33,6 +35,7 @@ def size(self): """返回队列的大小""" return len(self.items) + if __name__ == "__main__": deque = Deque() deque.add_front(1) diff --git "a/5.1\345\206\222\346\263\241\346\216\222\345\272\217.py" "b/5.1\345\206\222\346\263\241\346\216\222\345\272\217.py" index df17480..36dfb09 100644 --- "a/5.1\345\206\222\346\263\241\346\216\222\345\272\217.py" +++ "b/5.1\345\206\222\346\263\241\346\216\222\345\272\217.py" @@ -5,14 +5,15 @@ 稳定性:稳定 """ -def bubble_sort(alist): - for j in range(len(alist) - 1, 0, -1): + +def bubble_sort(a_list): + for j in range(len(a_list) - 1, 0, -1): # j表示每次遍历需要比较的次数,是逐渐减小的 for i in range(j): - if alist[i] > alist[i+1]: - alist[i], alist[i+1] = alist[i+1], alist[i] + if a_list[i] > a_list[i+1]: + a_list[i], a_list[i+1] = a_list[i+1], a_list[i] -li = [54,26,93,17,77,31,44,55,20] +li = [54, 26, 93, 17, 77, 31, 44, 55, 20] bubble_sort(li) print(li) diff --git "a/5.2\351\200\211\346\213\251\346\216\222\345\272\217.py" "b/5.2\351\200\211\346\213\251\346\216\222\345\272\217.py" index 8047c96..e626365 100644 --- "a/5.2\351\200\211\346\213\251\346\216\222\345\272\217.py" +++ "b/5.2\351\200\211\346\213\251\346\216\222\345\272\217.py" @@ -5,19 +5,21 @@ 稳定性:不稳定 """ -def selection_sort(alist): + +def selection_sort(a_list): # 需要进行n-1次选择操作 - for i in range(len(alist) - 1): + for i in range(len(a_list) - 1): # 记录最小位置 min_index = i - for j in range(i + 1, len(alist)): - if alist[j] < alist[min_index]: + for j in range(i + 1, len(a_list)): + if a_list[j] < a_list[min_index]: min_index = j # 如果选择出的数据不在正确的位置,进行交换 if min_index != i: - alist[i], alist[min_index] = alist[min_index], alist[i] + a_list[i], a_list[min_index] = a_list[min_index], a_list[i] + -alist = [54,226,93,17,77,31,44,55,20] -print("origin alist:",alist) -selection_sort(alist) -print("selection_sort:",alist) +a_list = [54, 226, 93, 17, 77, 31, 44, 55, 20] +print("origin a_list:", a_list) +selection_sort(a_list) +print("selection_sort:", a_list) diff --git "a/5.3\346\217\222\345\205\245\346\216\222\345\272\217.py" "b/5.3\346\217\222\345\205\245\346\216\222\345\272\217.py" index 8b0578d..e63bfa7 100644 --- "a/5.3\346\217\222\345\205\245\346\216\222\345\272\217.py" +++ "b/5.3\346\217\222\345\205\245\346\216\222\345\272\217.py" @@ -5,14 +5,14 @@ 稳定性:稳定 """ -def insert_sort(alist): +def insert_sort(a_list): # 从第二个位置起,即下标为1的元素开始向前插入 - for i in range(1, len(alist)): + for i in range(1, len(a_list)): # 从第i个元素开始向前比较,如果小于前一个元素,交换位置 for j in range(i, 0, -1): - if alist[j] < alist[j-1]: - alist[j], alist[j-1] = alist[j-1], alist[j] -alist = [54,26,93,17,77,31,44,55,20] -print("origin:",alist) -insert_sort(alist) -print("insert_sort:",alist) + if a_list[j] < a_list[j-1]: + a_list[j], a_list[j-1] = a_list[j-1], a_list[j] +a_list = [54,26,93,17,77,31,44,55,20] +print("origin:",a_list) +insert_sort(a_list) +print("insert_sort:",a_list) diff --git "a/5.4\345\277\253\351\200\237\346\216\222\345\272\217.py" "b/5.4\345\277\253\351\200\237\346\216\222\345\272\217.py" index 64adccf..235f45e 100644 --- "a/5.4\345\277\253\351\200\237\346\216\222\345\272\217.py" +++ "b/5.4\345\277\253\351\200\237\346\216\222\345\272\217.py" @@ -7,7 +7,7 @@ """ -def quick_sort(alist, start, end): +def quick_sort(a_list, start, end): """快速排序""" # 递归的退出条件 @@ -15,7 +15,7 @@ def quick_sort(alist, start, end): return # 设定起始元素为要寻找位置的基准元素 - mid = alist[start] + mid = a_list[start] # low 为序列左边的由左向右移动的游标 low = start @@ -25,28 +25,28 @@ def quick_sort(alist, start, end): while low < high: # 如果low与high未重合,high指向的元素不比基准元素小,则high向左移动 - while low < high and alist[high] >= mid: + while low < high and a_list[high] >= mid: high -= 1 # 将high指向的元素放到low的位置上 - alist[low] = alist[high] + a_list[low] = a_list[high] # 如果low与high未重合,low指向的元素比基准元素小,则low向右移动 - while low < high and alist[low] < mid: + while low < high and a_list[low] < mid: low += 1 # 将low指向的元素放到high的位置 - alist[high] = alist[low] + a_list[high] = a_list[low] # 退出循环后,low与high重合,此时所指位置为基准元素的正确位置 # 将基准元素放到该位置 - alist[low] = mid + a_list[low] = mid # 对基准元素左边的子序列进行快速排序 - quick_sort(alist, start, low - 1) + quick_sort(a_list, start, low - 1) # 对基准元素的右边子序列进行快速排序 - quick_sort(alist, low + 1, end) + quick_sort(a_list, low + 1, end) -alist = [54,26,93,17,77,31,44,55,20] -print("origin:",alist) -quick_sort(alist, 0, len(alist) - 1) -print("quick_sort:",alist) \ No newline at end of file +a_list = [54,26,93,17,77,31,44,55,20] +print("origin:",a_list) +quick_sort(a_list, 0, len(a_list) - 1) +print("quick_sort:",a_list) \ No newline at end of file diff --git "a/5.5\345\270\214\345\260\224\346\216\222\345\272\217.py" "b/5.5\345\270\214\345\260\224\346\216\222\345\272\217.py" index 6f2d048..fd28a9f 100644 --- "a/5.5\345\270\214\345\260\224\346\216\222\345\272\217.py" +++ "b/5.5\345\270\214\345\260\224\346\216\222\345\272\217.py" @@ -4,26 +4,26 @@ 时间复杂度:O(n**2) """ -def shell_sort(alist): - n = len(alist) +def shell_sort(a_list): + n = len(a_list) # 初始步长 gap = n//2 while gap > 0: # 按步长进行插入排序 for i in range(gap, n): - #print("i:",i,alist[i]) + # print("i:",i,a_list[i]) j = i # 插入排序 - while j >= gap and alist [j - gap] > alist[j]: - #print("j",alist[j - gap], alist[j]) - alist[j - gap], alist[j] = alist[j], alist[j - gap] + while j >= gap and a_list [j - gap] > a_list[j]: + # print("j",a_list[j - gap], a_list[j]) + a_list[j - gap], a_list[j] = a_list[j], a_list[j - gap] j -= gap # 得到新的步长 gap = gap//2 -alist = [54,26,93,17,77,31,44,55,20] -print("origin:",alist) -shell_sort(alist) -print("shell_sort:",alist) +a_list = [54,26,93,17,77,31,44,55,20] +print("origin:",a_list) +shell_sort(a_list) +print("shell_sort:",a_list) diff --git "a/5.6.\345\275\222\345\271\266\346\216\222\345\272\217.py" "b/5.6.\345\275\222\345\271\266\346\216\222\345\272\217.py" index 3d84ae0..87576a1 100644 --- "a/5.6.\345\275\222\345\271\266\346\216\222\345\272\217.py" +++ "b/5.6.\345\275\222\345\271\266\346\216\222\345\272\217.py" @@ -5,13 +5,13 @@ 稳定性:稳定 """ -def merge_sort(alist): - if len(alist) <= 1: - return alist +def merge_sort(a_list): + if len(a_list) <= 1: + return a_list # 二次分解 - mid = len(alist)//2 - left = merge_sort(alist[:mid]) - right = merge_sort(alist[mid:]) + mid = len(a_list)//2 + left = merge_sort(a_list[:mid]) + right = merge_sort(a_list[mid:]) # 合并 return merge(left, right) @@ -31,6 +31,6 @@ def merge(left, right): result += right[r:] return result -alist = [54,26,93,17,77,31,44,55,20] -print("origin:", alist) -print("merge_sort:",merge_sort(alist)) +a_list = [54,26,93,17,77,31,44,55,20] +print("origin:", a_list) +print("merge_sort:",merge_sort(a_list)) diff --git "a/6.\344\272\214\345\210\206\346\237\245\346\211\276.py" "b/6.\344\272\214\345\210\206\346\237\245\346\211\276.py" index 5dbe95f..354fdfa 100644 --- "a/6.\344\272\214\345\210\206\346\237\245\346\211\276.py" +++ "b/6.\344\272\214\345\210\206\346\237\245\346\211\276.py" @@ -4,14 +4,14 @@ """ # 法1.非递归实现 -def binary_search(alist, item): +def binary_search(a_list, item): first = 0 - last = len(alist) - 1 + last = len(a_list) - 1 while first <= last: midpoint = (first + last)//2 - if alist[midpoint] == item: + if a_list[midpoint] == item: return True - elif item < alist[midpoint]: + elif item < a_list[midpoint]: last = midpoint - 1 else: first = midpoint + 1 @@ -27,18 +27,18 @@ def binary_search(alist, item): # ********************************************************* # 法2.递归实现 -def binary_search_re(alist, item): - if len(alist) == 0: +def binary_search_re(a_list, item): + if len(a_list) == 0: return False else: - midpoint = len(alist)//2 - if alist[midpoint] == item: + midpoint = len(a_list)//2 + if a_list[midpoint] == item: return True else: - if item < alist[midpoint]: - return binary_search_re(alist[:midpoint], item) + if item < a_list[midpoint]: + return binary_search_re(a_list[:midpoint], item) else: - return binary_search_re(alist[midpoint + 1:], item) + return binary_search_re(a_list[midpoint + 1:], item) testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,] print("origin list:",testlist)