随笔分类 - 常用数据结构与算法
大学基础
摘要:1 from collections import deque 2 3 4 class Tree(object): 5 class TreeNode(object): 6 def __init__(self, data): 7 self.data = data 8 self.lchild = Non
阅读全文
摘要:1 from 数据结构.链表的实现 import Link 2 3 4 # 类似于集合的哈希表 5 class HashTable(object): 6 def __init__(self, size=100): 7 self.size = size 8 self.table = [Link() f
阅读全文
摘要:1 class Link: 2 class Node: 3 def __init__(self, item, next=None): 4 self.item = item 5 self.next = next 6 7 class LinkListIterator: 8 def __init__(se
阅读全文
摘要:1 class Queue(): 2 def __init__(self, size=100): 3 self.rear = 0 4 self.front = 0 5 self.size = size + 1 6 self.li = [0 for _ in range(self.size)] 7 8
阅读全文
摘要:1 class Stack: 2 def __init__(self): 3 self.lis = [] 4 5 def pop(self): 6 if not self.is_empty(): 7 return self.lis.pop() 8 else: 9 raise IndexError("
阅读全文
摘要:1 from collections import deque 2 3 maze = [ 4 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 5 [1, 0, 0, 1, 0, 0, 0, 1, 0, 1], 6 [1, 0, 0, 1, 0, 0, 0, 1, 0, 1], 7 [
阅读全文
摘要:1 maze = [ 2 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 3 [1, 0, 0, 1, 0, 0, 0, 1, 0, 1], 4 [1, 0, 0, 1, 0, 0, 0, 1, 0, 1], 5 [1, 0, 0, 0, 0, 1, 1, 0, 0, 1], 6 [
阅读全文
摘要:1 # 为了返回找到列表的下标,故而携带两个参数 2 def binary_search(num, my_list, left, right): 3 if left == right: 4 return left if my_list[left] == num else f'没找到{num}' 5
阅读全文
摘要:1 import random 2 from cal_time import cal_time 3 4 5 @cal_time 6 def insert_sort(_list): 7 for i in range(len(_list) - 1): 8 j = i 9 next_num = _list
阅读全文
摘要:1 import random 2 3 4 def bubbling_sort(_list): 5 length_1 = len(_list)-1 6 for i in range(length_1): 7 is_exchange = False 8 for j in range(length_1-
阅读全文
摘要:1 import random 2 3 4 def select_sort(_lis): 5 for i in range(len(_lis)-1): 6 min_indx = i 7 for j in range(i+1, len(_lis)): 8 if _lis[min_indx] > _li
阅读全文
摘要:1 import random 2 3 4 def merge(mer_l, mer_r): 5 new_list = [] 6 i = 0 7 j = 0 8 len_l = len(mer_l) 9 len_r = len(mer_r) 10 while len_l > i and len_r
阅读全文
摘要:1 import random 2 3 4 def sift(li, low, high): 5 if low < high: 6 i = low 7 j = 2 * i + 1 8 temp = li[low] 9 while j <= high: 10 if j+1 <= high and li
阅读全文
摘要:1 def quick_sort(lis, l, r): 2 if l < r: 3 mid = partition(lis, l, r) 4 quick_sort(lis, l, mid-1) 5 quick_sort(lis, mid+1, r) 6 7 8 def partition(lis,
阅读全文