python数据结构和算法知识点汇总

1.时间复杂度:算法A处理规模为n的问题示例所用时间 T ( n ) = O ( g ( n ) ) T(n)=O(g(n)) T(n)=O(g(n)),记为 T ( n ) T(n) T(n)
2.最坏时间复杂度:算法完成工作最多需要多少基本操作
3.程序=数据结构+算法
4.算法是为了解决实际问题而设计的,数据结构是算法需要处理的问题的载体
5.常用的数据运算:插入 删除 修改 查找 排序
6.顺序表:将元素顺序地存放在一块连续的存储区里,元素间的顺序关系由它们的存储顺序 自然表示
7.链表:将元素存放在通过链接构造起来的一系列存储块中
8.顺序表组成:逻辑地址 元素存储 物理地址,如果元素大小不一致,可以使用元素外置顺序表
9.python中list是一种采用分离式技术实现的动态顺序表
10.为什么需要链表:顺序表主要预先知道数据大小 来申请连续空间,扩充时又要数据搬迁,不是很灵活,链表充分利用计算机空间,实现灵活的内存动态管理
11.单链表:每个节点包含两个域:信息域(元素域)和连接域,指向下个节点,最后一个节点指向空值
12.节点实现

class Node(object):
	def __init__(self, elem):
		self.elem = elem
		self.next = None

13.链表一般有:单向链表 单向循环链表 双向链表
14.栈(stack):后进先出last in first out

class Stack(object):
    def __init__(self):
        self.items = []
        
    def is_empty(self):
        return self.items == []
        
    def push(self, item):
        self.items.append(item)
        
    def pop(self):
        return self.items.pop()
    
    def peek(self):
        return self.items[len(self.items)-1]
    
    def size(self):
        return len(self.items)

if __name__ == "__main":
    pass

15.队列(queue):first in first out

class Queue(object):
    def __init__(self):
        self.items = []
        
    def is_empty(self):
        return self.items == []
        
    def enqueue(self, item):
        self.items.append(item)
        
    def dequeue(self):
        return self.items.pop(0)
    
    def size(self):
        return len(self.items)

if __name__ == "__main__":
    pass

16.排序算法的稳定性:相等键值在排序前后位置不变
17.冒泡排序(Bubble sort):

def bubble_sort(alist):
    for j in range(len(alist)-1,0,-1):
        for i in range(j):
            if alist[i] > alist[i+1]:
                alist[i], alist[i+1] = alist[i+1], alist[i]
li = [54,26,93,17,77,31,44,55,20]
bubble_sort(li)
print(li)
# 稳定

18.选择排序(Selection sort):找到未排序最小的,交换放在未排序起始位置

def select_sort(alist):
    n = len(alist)
    for j in range(n-1):
        min_index = j
        for i in range(j+1, n-1):
            if alist[min_index] > alist[i+1]:
                min_index = i+1
        alist[j],alist[min_index] = alist[min_index], alist[j]

li = [54,26,93,17,77,31,44,55,20]
select_sort(li)
print(li)
# 不稳定

19.插入排序(insertion sort):构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入

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]
li = [54,26,93,17,77,31,44,55,20]
insert_sort(li)
print(li)
# 稳定

20.希尔排序(shell sort):把记录按下标的一定增量分组,对每组进行直接插入排序,直至增量减至1时,算法结束

def shell_sort(alist):
    n = len(alist)
    gap = n//2
    while gap > 0:
        for i in range(gap, n):
            j = i
            while j>=gap and alist[j-gap]>alist[j]:
                alist[j-gap],alist[j] = alist[j], alist[j-gap]
                j -= gap
            gap = gap // 2
            
li = [54,26,93,17,77,31,44,55,20]
shell_sort(li)
print(li) 
# 不稳定

21.归并排序:先递归分解数组,再合并数组

def merge(left, right):
    i, j = 0, 0
    result = []
    while i<len(left) and j<len(right):
        if left[i] < right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]
    result += right[j:]
    return result

def merge_sort(lists):
    lens = len(lists)
    if lens <= 1:
        return lists
    num = lens//2
    left = merge_sort(lists[:num])
    right = merge_sort(lists[num:])
    return merge(left, right)
if __name__ ==  "__main__":
    alist = [3, 4, 2, 8, 9, 5, 1]
    new_list = merge_sort(alist)
    print(new_list)
# 稳定

22.快速排序quick sort

def quick_sort(alist, start, end):
    if start >=end:
        return
    low, high = start, end
    mid_num = alist[low]
    while low < high:
        while low < high and alist[high] >= mid_num:
            high -= 1
        alist[low] = alist[high]
        while low < high and alist[low] <= mid_num:
            low += 1
        alist[high] = alist[low]
    alist[low] = mid_num
    print(alist)
    quick_sort(alist, start,low-1)
    quick_sort(alist, low+1, end)
        

if __name__ == "__main__":
    alist = [10,3, 4, 2, 8, 9, 5, 1, 6,15,100,54,36,87,16]
    quick_sort(alist, 0, len(alist)-1)

23.二叉树

class Node(object):
    """节点类"""
    def __init__(self, elem):
        self.elem = elem
        self.lchild = lchild
        self.rchild = rchild


class Tree(object):
    def __init__(self):
        self.root = None
    
    def add(self, item):
        node = Node(item)
        if self.root is None:
            self.root = node
            return
        queue = [self.root]
        while queue:
            cur_node = queue.pop(0)
            if cur_node.lchild is None:
                cur_node.lchild = node
                return
            else:
                queue.append(cur_node.lchild)
            if cur_node.rchild is None:
                cur_node.rchild = node
                return
            else:
                queue.append(cur_node.rchild)          
      # 广度优先遍历      
    def breath_travel(self):
        if self.root is None:
            return
        queue = []
        queue.append(self.root)
        while queue:
            cur_node = queue.pop(0)
            print(cur_node.elem, end=" ")
            if cur_node.lchild is not None:
                queue.append(cur_node.lchild)
            if cur_node.rchild is not None:
                queue.append(cur_node.rchild)
     # 深度优先遍历(先序遍历)
    def preorder(self, node):
        if node is None:
            return
        print(node.elem, end=" ")
        self.preorder(node.lchild)
        self.preorder(node.rchild)

24.深度优先遍历(先序,中序,后序):有中序及任何另外一种遍历方式可以确定唯一树,因为中序可以将左右分开
25.堆排序:1是建堆,2是排序,一般在小规模数据中不适用,在大规模数据中适用,性能优越

你可能感兴趣的:(算法,数据结构,排序算法,python)