数据结构与算法必备的 50 个代码实现

186 篇文章 0 订阅
97 篇文章 3 订阅

重磅干货,第一时间送达

数据结构和算法是程序员的内功心法和基本功。无论是人工智能还是其它计算机科学领域,掌握扎实的数据结构和算法知识,往往会助力不少!今天给大家推荐一份不错的数据结构与算法资源。特点是:全代码实现!

这份资源的作者王争老师是前 Google 工程师,5 万+人跟着学的《数据结构和算法之美》专栏作者。他总结了程序员必备的 50 个数据结构与算法,以及相应的代码实现。开源地址为:

https://github.com/wangzheng0822/algo

我们来看一下这必备的 50 个数据结构与算法究竟包含了哪些内容。

数组 

  • 问题:实现一个支持动态扩容的数组

  • 问题:实现一个大小固定的有序数组,支持动态增删改操作

  • 问题:实现两个有序数组合并为一个有序数组

 

链表 

  • 问题:实现单链表、循环链表、双向链表,支持增删操作

  • 问题:实现单链表反转

  • 问题:实现两个有序的链表合并为一个有序链表

  • 问题:实现求链表的中间结点

 

栈 

  • 问题:用数组实现一个顺序栈

  • 问题:用链表实现一个链式栈

  • 问题:编程模拟实现一个浏览器的前进、后退功能

 

队列 

  • 问题:用数组实现一个顺序队列

  • 问题:用链表实现一个链式队列

  • 问题:实现一个循环队列

 

递归 

  • 问题:编程实现斐波那契数列求值f(n)=f(n-1)+f(n-2)

  • 问题:编程实现求阶乘n!

  • 问题:编程实现一组数据集合的全排列

 

排序 

  • 问题:实现归并排序、快速排序、插入排序、冒泡排序、选择排序

  • 问题:编程实现O(n)时间复杂度内找到一组数据的第K大元素

 

二分查找 

  • 问题:实现一个有序数组的二分查找算法

  • 问题:实现模糊二分查找算法(比如大于等于给定值的第一个元素)

 

散列表 

  • 问题:实现一个基于链表法解决冲突问题的散列表

  • 问题:实现一个LRU缓存淘汰算法

 

字符串 

  • 问题:实现一个字符集,只包含a~z这26个英文字母的Trie树

  • 问题:实现朴素的字符串匹配算法

 

二叉树 

  • 问题:实现一个二叉查找树,并且支持插入、删除、查找操作

  • 问题:实现查找二叉查找树中某个节点的后继、前驱节点

  • 问题:实现二叉树前、中、后序以及按层遍历

 

堆 

  • 问题:实现一个小顶堆、大顶堆、优先级队列

  • 问题:实现堆排序

  • 问题:利用优先级队列合并K个有序数组

  • 问题:求一组动态数据集合的最大Top K

 

图 

  • 问题:实现有向图、无向图、有权图、无权图的邻接矩阵和邻接表表示方法

  • 问题:实现图的深度优先搜索、广度优先搜索

  • 问题:实现Dijkstra算法、A*算法

  • 问题:实现拓扑排序的Kahn算法、DFS算法

 

回溯 

  • 问题:利用回溯算法求解八皇后问题

  • 问题:利用回溯算法求解0-1背包问题

 

分治 

  • 问题:利用分治算法求一组数据的逆序对个数

 

动态规划 

  • 问题:0-1背包问题

  • 问题:最小路径和

  • 问题:编程实现莱文斯坦最短编辑距离

  • 问题:编程实现查找两个字符串的最长公共子序列

  • 问题:编程实现一个数据序列的最长递增子序列

 

以上所有的数据结构与算法都配备相应的程序代码。一大特点是代码配备多种编程语言,例如 Python、C、Java、Go、Scala 等。

举例来说,关于常见的排序算法,例如冒泡排序、插入排序、选择排序,作者给出了相应的 Python 代码实现:

""" Bubble sort, insertion sort and selection sort 冒泡排序、插入排序、选择排序 Author: Wenru"""from typing import List# 冒泡排序def bubble_sort(a: List[int]): length = len(a) if length <= 1: return for i in range(length): made_swap = False for j in range(length - i - 1): if a[j] > a[j + 1]: a[j], a[j + 1] = a[j + 1], a[j] made_swap = True if not made_swap: break# 插入排序def insertion_sort(a: List[int]): length = len(a) if length <= 1: return for i in range(1, length): value = a[i] j = i - 1 while j >= 0 and a[j] > value: a[j + 1] = a[j] j -= 1 a[j + 1] = value# 选择排序def selection_sort(a: List[int]): length = len(a) if length <= 1: return for i in range(length): min_index = i min_val = a[i] for j in range(i, length): if a[j] < min_val: min_val = a[j] min_index = j a[i], a[min_index] = a[min_index], a[i]def test_bubble_sort(): test_array = [1, 1, 1, 1] bubble_sort(test_array) assert test_array == [1, 1, 1, 1] test_array = [4, 1, 2, 3] bubble_sort(test_array) assert test_array == [1, 2, 3, 4] test_array = [4, 3, 2, 1] bubble_sort(test_array) assert test_array == [1, 2, 3, 4]def test_insertion_sort(): test_array = [1, 1, 1, 1] insertion_sort(test_array) assert test_array == [1, 1, 1, 1] test_array = [4, 1, 2, 3] insertion_sort(test_array) assert test_array == [1, 2, 3, 4] test_array = [4, 3, 2, 1] insertion_sort(test_array) assert test_array == [1, 2, 3, 4]def test_selection_sort(): test_array = [1, 1, 1, 1] selection_sort(test_array) assert test_array == [1, 1, 1, 1] test_array = [4, 1, 2, 3] selection_sort(test_array) assert test_array == [1, 2, 3, 4] test_array = [4, 3, 2, 1] selection_sort(test_array) assert test_array == [1, 2, 3, 4]if __name__ == "__main__": array = [5, 6, -1, 4, 2, 8, 10, 7, 6] bubble_sort(array) print(array) array = [5, 6, -1, 4, 2, 8, 10, 7, 6] insertion_sort(array) print(array) array = [5, 6, -1, 4, 2, 8, 10, 7, 6] selection_sort(array) print(array)

 

当然,还有其它编程语言的代码实现,这里就不一一列举了!

目前,这份资源已经在 GitHub 上收获了 6000 星了。是一份不错的数据结构与算法参考代码。值得一看。最后再附上地址:

https://github.com/wangzheng0822/algo

 

品略图书馆 http://www.pinlue.com/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
16进制10进制.txt 32.txt asm.txt Crctable.txt C标志符命名源程序.txt erre.txt erre2.txt ff.txt for循环的.txt list.log N皇后问题回溯算法.txt ping.txt re.txt source.txt winsock2.txt ww.txt 万年历.txt 万年历的算法 .txt 乘方函数桃子猴.txt 乘法矩阵.txt 二分查找1.txt 二分查找2.txt 二叉排序树.txt 二叉树.txt 二叉树实例.txt 二进制数.txt 二进制数2.txt 余弦曲线.txt 余弦直线.txt 傻瓜递归.txt 冒泡排序.txt 冒泡法改进.txt 动态计算网络最长最短路线.txt 十五人排序.txt 单循环链表.txt 单词倒转.txt 单链表.txt 单链表1.txt 单链表2.txt 单链表倒序.txt 单链表的处理全集.txt 双链表正排序.txt 反出字符.txt 叠代整除.txt 各种排序法.txt 哈夫曼算法.txt 哈慢树.txt 四分砝码.txt 四塔1.txt 四塔2.txt 回文.txt 图.txt 圆周率.txt 多位阶乘.txt 多位阶乘2.txt 大加数.txt 大小倍约.txt 大整数.txt 字符串查找.txt 字符编辑.txt 字符编辑技术(插入和删除) .txt 完数.txt 定长串.txt 实例1.txt 实例2.txt 实例3.txt 小写数字转换成大写数字1.txt 小写数字转换成大写数字2.txt 小写数字转换成大写数字3.txt 小字库DIY-.txt 小字库DIY.txt 小孩分糖果.txt 小明买书.txt 小白鼠钻迷宫.txt 带头结点双链循环线性表.txt 平方根.txt 建树和遍历.txt 建立链表1.txt 扫描码.txt 挽救软盘.txt 换位递归.txt 排序法.txt 推箱子.txt 数字移动.txt 数据结构.txt 数据结构2.txt 数据结构3.txt 数组完全单元.txt 数组操作.txt 数组递归退出.txt 数组递归退出2.txt 文件加密.txt 文件复制.txt 文件连接.txt 无向图.txt 时间陷阱.txt 杨辉三角形.txt 栈单元加.txt 栈操作.txt 桃子猴.txt 桶排序.txt 检出错误.txt 检测鼠标.txt 汉字字模.txt 汉诺塔.txt 汉诺塔2.txt 灯塔问题.txt 猴子和桃.txt 百鸡百钱.txt 矩阵乘法动态规划.txt 矩阵转换.txt 硬币分法.txt 神经元模型.txt 穷举搜索法.txt 符号图形.txt 简单数据库.txt 简单计算器.txt 简单逆阵.txt 线性顺序存储结构.txt 线索化二叉树.txt 绘制圆.txt 编随机数.txt 网络最短路径Dijkstra算法.txt 自我复制.txt 节点.txt 苹果分法.txt 螺旋数组1.txt 螺旋数组2.txt 试题.txt 诺汉塔画图版.txt 读写文本文件.txt 货郎担分枝限界图形演示.txt 货郎担限界算法.txt 质因子.txt 输出自已.txt 迷宫.txt 迷宫问题.txt 逆波兰计算器.txt 逆矩阵.txt 逆阵.txt 递堆法.txt 递归桃猴.txt 递归车厢.txt 递推.txt 逻辑移动.txt 链串.txt 链栈.txt 链表十五人排序.txt 链表(递归).txt 链队列.txt 队列.txt 阶乘递归.txt 阿姆斯特朗数.txt 非递归.txt 顺序栈.txt 顺序表.txt 顺序队列.txt 骑士遍历1.txt 骑士遍历2.txt 骑士遍历回逆.txt 黑白.txt
全集内容结构如下: ├─图 │ ├─关键路径(有向无环图及其应用2) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ CriticalPath.cpp │ │ CriticalPath.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的关节点 │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ FindArticul.cpp │ │ FindArticul.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的数组表示法 │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的遍历 │ │ ALGraph.cpp │ │ ALGraph.h │ │ DEBUG.txt │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MTraverse.cpp │ │ MTraverse.h │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的邻接表存储结构 │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─最短路径(从某个源点到其余各顶点的的最短路径) │ │ 1.txt │ │ 2.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ ShortestPath_DIJ.cpp │ │ ShortestPath_DIJ.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─最短路径(每一对顶点间的最短路径) │ 1.txt │ 2.txt │ InfoType.cpp │ InfoType.h │ Main.cpp │ map.txt │ MGraph.cpp │ MGraph.h │ RailwaySearch.cpp │ ShortestPath_FLOYD.cpp │ ShortestPath_FLOYD.h │ Status.h │ VertexType.cpp │ VertexType.h │ ├─排序 │ ├─冒泡排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_BubbleSort.cpp │ │ Sq_BubbleSort.h │ │ │ ├─哈希表(哈希查找) │ │ ElemType.cpp │ │ ElemType.h │ │ HashTable.cpp │ │ HashTable.h │ │ main.cpp │ │ Records.txt │ │ │ ├─基数排序 │ │ 1.txt │ │ main.cpp │ │ SLL_RadixSort.cpp │ │ SLL_RadixSort.h │ │ │ ├─归并排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ sq_MergeSort.cpp │ │ sq_MergeSort.h │ │ │ ├─快速排序 │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_QuitSort.cpp │ │ Sq_QuitSort.h │ │ │ ├─拓扑排序(有向无环图及其应用) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ TopologicalSort.cpp │ │ TopologicalSort.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─插入排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(希尔排序) │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(表插入排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ SL_InsertSort.cpp │ │ SL_InsertSort.h │ │ │ ├─选择排序(堆排序) │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_HeapSort.cpp │ │ Sq_HeapSort.h │ │ │ ├─选择排序(树形选择排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_TreeSelectionSort.cpp │ │ Sq_TreeSelectionSort.h │ │ │ └─选择排序(简单选择排序) │ 1.txt │ main.cpp │ RedType.cpp │ RedType.h │ Sq_SelectSort.cpp │ Sq_SelectSort.h │ ├─查找 │ ├─动态查找表(二叉排序树) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡二叉树(二叉排序树的平衡旋转生成) │ │ 1.txt │ │ BBSTree.cpp │ │ BBSTree.h │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡的m路查找树—B_树 │ │ BTree.cpp │ │ BTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─键树(Trie树) │ │ 1.txt │ │ main.cpp │ │ Record.h │ │ Status.h │ │ TrieTree.cpp │ │ TrieTree.h │ │ │ ├─键树(双链键树) │ │ 1.txt │ │ DLTree.cpp │ │ DLTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─静态查找表(有序表的查找) │ │ 1.txt │ │ main.cpp │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ │ ├─静态查找表(静态树表查找) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ TElmeType.h │ │ │ └─静态查找表(顺序表的查找) │ 1.txt │ main.cpp │ SElemType.cpp │ SElemType.h │ SSTable.cpp │ SSTable.h │ Status.h │ ├─树 │ ├─二叉树的二叉链表存储 │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─二叉树的顺序存储结构 │ │ main.cpp │ │ SqBiTree.cpp │ │ SqBiTree.h │ │ Status.h │ │ TELemType_define.cpp │ │ │ ├─哈夫曼树和哈夫曼编码 │ │ HuffmanTree.cpp │ │ HuffmanTree.h │ │ main.cpp │ │ Status.h │ │ │ ├─最小生成树 │ │ 1.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MiniSpanTree_Kruskal.cpp │ │ MiniSpanTree_Kruskal.h │ │ MiniSpanTree_PRIM.cpp │ │ MiniSpanTree_PRIM.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─树的二叉链表 │ │ CSTree.cpp │ │ CSTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─深度优先生成森林(无向图的连通性和生成树) │ │ ALGraph.cpp │ │ ALGraph.h │ │ CSTree.cpp │ │ CSTree.h │ │ DFSForest.cpp │ │ DFSForest.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─线索二叉树 │ BiThrTree.cpp │ BiThrTree.h │ main.cpp │ Status.h │ TElmeType.h │ └─表和数组 ├─KMP算法 │ Basic_operation_functions.h │ def_SString.h │ KMP.h │ main.cpp │ Status.h │ ├─n阶对称矩阵的压缩存储 │ Basic_operation_functions.h │ mian.cpp │ Status.h │ struct SyMatrix.h │ ├─三元组稀疏矩阵的压缩存储 │ Basic_operation_functions.h │ B_opera_fun_called_fun.h │ main.cpp │ Status.h │ struct TSMatrix.h │ Universal_main.h │ Universa_ts_b_opera_fun.h │ ├─不设头结点的单链表 │ LinkList.cpp │ LinkList.h │ main.cpp │ Status.h │ ├─串的堆存储结构 │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─串的定长顺序存储结构 │ Basic_operation_functions.h │ def_SString.h │ Main.cpp │ Status.h │ ├─广义表 │ GList.cpp │ GList.h │ main.cpp │ SString.cpp │ SString.h │ Status.h │ ├─数组 │ Basic_operation_functions.h │ main.cpp │ Status.h │ struct array.h │ ├─文本编辑(串和行表操作) │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─栈的顺序存储结构 │ main.cpp │ SqStack.cpp │ SqStack.h │ Status.h │ ├─走迷宫 │ Basic_called_functions.h │ Basic_operation_functions.h │ Main_Maze.cpp │ Status.h │ struct SqStack.h │ └─链队列 Basic_called_functions.cpp Basic_called_functions.h Basic_operation_functions.cpp main.cpp QElemType.h Status.h Struct_LinkQueue.h

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值