diff --git a/.gitignore b/.gitignore index fbf1224ca..b51d9e5b4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,10 @@ .vscode/ # mkdocs files -overrides/ site/ +.cache/ +overrides/ codes/python codes/cpp + docs/chapter_* \ No newline at end of file diff --git a/README.md b/README.md index d36e38183..9920407f8 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,10 @@ -# Welcome to MkDocs +# Hello 算法 -For full documentation visit [mkdocs.org](https://www.mkdocs.org). +[](krahets.github.io/hello-algo/) -## Commands +动画图解、能运行、可讨论的 -* `mkdocs new [dir-name]` - Create a new project. -* `mkdocs serve` - Start the live-reloading docs server. -* `mkdocs build` - Build the documentation site. -* `mkdocs -h` - Print help message and exit. - -## Project layout - - mkdocs.yml # The configuration file. - docs/ - index.md # The documentation homepage. - ... # Other markdown pages, images and other files. +数据结构与算法快速入门教程 ## 更新日志 @@ -23,7 +13,7 @@ For full documentation visit [mkdocs.org](https://www.mkdocs.org). | 新增:算法无处不在 | 2022-10-10 | | 新增:数组与链表 | 2022-10-15 | | 新增:数据结构简介 | 2022-10-20 | -| 新增:前言 | 2022-10-23 | +| 新增:前言 | 2022-10-23 | | 新增:计算复杂度 | 2022-11-03 | | 更新:配图 | 2022-11-04 | | 新增:数据与内存 | 2022-11-05 | @@ -36,6 +26,7 @@ For full documentation visit [mkdocs.org](https://www.mkdocs.org). | 更新:首页介绍 | 2022-11-15 | | 更新:关于本书
新增:如何使用本书
新增:一起参与创作 | 2022-11-16 | | 新增:查找算法 | 2022-11-19 | +| 更新:Markdown Stylesheet
新增:冒泡排序、插入排序 | 2022-11-21 | ## License diff --git a/codes/java/chapter_sorting/bubble_sort.java b/codes/java/chapter_sorting/bubble_sort.java new file mode 100644 index 000000000..e5ffb1b6c --- /dev/null +++ b/codes/java/chapter_sorting/bubble_sort.java @@ -0,0 +1,50 @@ +package chapter_sorting; + +import java.util.*; + +public class bubble_sort { + /* 冒泡排序 */ + static void bubbleSort(int[] nums) { + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (int i = nums.length - 1; i > 0; i--) { + // 内循环:冒泡操作 + for (int j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + int tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + } + } + } + } + + /* 冒泡排序(标志优化)*/ + static void bubbleSortWithFlag(int[] nums) { + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for (int i = nums.length - 1; i > 0; i--) { + boolean flag = false; // 初始化标志位 + // 内循环:冒泡操作 + for (int j = 0; j < i; j++) { + if (nums[j] > nums[j + 1]) { + // 交换 nums[j] 与 nums[j + 1] + int tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + flag = true; // 记录交换元素 + } + } + if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出 + } + } + + public static void main(String[] args) { + int[] nums = { 4, 1, 3, 1, 5, 2 }; + bubbleSort(nums); + System.out.println("排序后数组 nums = " + Arrays.toString(nums)); + + int[] nums1 = { 4, 1, 3, 1, 5, 2 }; + bubbleSortWithFlag(nums1); + System.out.println("排序后数组 nums1 = " + Arrays.toString(nums)); + } +} diff --git a/codes/java/chapter_sorting/insertion_sort.java b/codes/java/chapter_sorting/insertion_sort.java new file mode 100644 index 000000000..2e8d26d32 --- /dev/null +++ b/codes/java/chapter_sorting/insertion_sort.java @@ -0,0 +1,25 @@ +package chapter_sorting; + +import java.util.*; + +public class insertion_sort { + /* 插入排序 */ + static void insertionSort(int[] nums) { + // 外循环:base = nums[1], nums[2], ..., nums[n-1] + for (int i = 1; i < nums.length; i++) { + int base = nums[i], j = i - 1; + // 内循环:将 base 插入到左边的正确位置 + while (j >= 0 && nums[j] > base) { + nums[j + 1] = nums[j]; // 1. 将 nums[j] 向右移动一位 + j--; + } + nums[j + 1] = base; // 2. 将 base 赋值到正确位置 + } + } + + public static void main(String[] args) { + int[] nums = { 4, 1, 3, 1, 5, 2 }; + insertionSort(nums); + System.out.println("排序后数组 nums = " + Arrays.toString(nums)); + } +} diff --git a/docs/index.assets/algorithm_animation.gif b/docs/index.assets/algorithm_animation.gif deleted file mode 100644 index 5db93eeee..000000000 Binary files a/docs/index.assets/algorithm_animation.gif and /dev/null differ diff --git a/docs/index.assets/animation.gif b/docs/index.assets/animation.gif new file mode 100644 index 000000000..6522ef9ff Binary files /dev/null and b/docs/index.assets/animation.gif differ diff --git a/docs/index.assets/comment.gif b/docs/index.assets/comment.gif index 13943cb46..d64787ac3 100644 Binary files a/docs/index.assets/comment.gif and b/docs/index.assets/comment.gif differ diff --git a/docs/index.assets/conceptual_rendering.jpg b/docs/index.assets/conceptual_rendering.jpg deleted file mode 100644 index ee98d5331..000000000 Binary files a/docs/index.assets/conceptual_rendering.jpg and /dev/null differ diff --git a/docs/index.assets/conceptual_rendering.png b/docs/index.assets/conceptual_rendering.png new file mode 100644 index 000000000..591d633be Binary files /dev/null and b/docs/index.assets/conceptual_rendering.png differ diff --git a/docs/index.assets/running_code.gif b/docs/index.assets/running_code.gif index 0d01ffbf8..7377773c1 100644 Binary files a/docs/index.assets/running_code.gif and b/docs/index.assets/running_code.gif differ diff --git a/docs/index.md b/docs/index.md index 219448fd9..b156ed10e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -7,34 +7,38 @@ hide: === " "
- ![conceptual_rendering](index.assets/conceptual_rendering.jpg){ align=left width=300 } -
+ ![conceptual_rendering](index.assets/conceptual_rendering.png){ align=left width=300 }


《 Hello,算法 》

动画图解、能运行、可讨论的
数据结构与算法快速入门教程

-

[![github-stars](https://img.shields.io/github/stars/krahets/hello-algo?style=social)](https://github.com/krahets/hello-algo)

+

[@Krahets](https://leetcode.cn/u/jyd/)

+

[![github-stars](https://img.shields.io/github/stars/krahets/hello-algo?style=social)](https://github.com/krahets/hello-algo)


--- -

「动画 + 图片 + HTML」

+

「清晰动画讲解」

-

重点知识以动画和图解为主,提升知识吸收效率
由 MkDocs 构建文档,支持笔记本、平板、手机多种终端

+

借助动画介绍重点,提升知识吸收效率
HTML 文档,支持笔记本、平板、手机多种终端

-![algorithm_animation](index.assets/algorithm_animation.gif) +![algorithm_animation](index.assets/animation.gif) -

「面向代码,注重实践」

+--- + +

「代码实践导向」

示例代码皆可一键运行,在调试中加深理解
提供 Java, C++, Python 源码与详细注释

![running_code](index.assets/running_code.gif) -

「可讨论,能提问」

+--- -

在评论区和小伙伴们一起讨论进步
作者定期回复评论问题(一般 < 72h )

+

「可讨论与提问」

+ +

在评论区与小伙伴们一起学习进步
作者定期回复评论问题(一般 < 72h )

![comment](index.assets/comment.gif) diff --git a/docs/stylesheets/extra.css b/docs/stylesheets/extra.css index 9ba484d36..98967bcb0 100644 --- a/docs/stylesheets/extra.css +++ b/docs/stylesheets/extra.css @@ -1,6 +1,7 @@ + +/* Color Settings */ /* https://github.com/squidfunk/mkdocs-material/blob/6b5035f5580f97532d664e3d1babf5f320e88ee9/src/assets/stylesheets/main/_colors.scss */ /* https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/#custom-colors */ - :root > * { --md-primary-fg-color: #FFFFFF; --md-primary-bg-color: #1D1D20; @@ -30,3 +31,14 @@ /* Reset alignment for table cells */ text-align: initial; } + + +/* Markdown Header */ +.md-typeset h1 { + font-weight: 400; + color: var(--md-default-fg-color); +} + +.md-typeset h2 { + font-weight: 400; +} diff --git a/mkdocs.yml b/mkdocs.yml index 8ada6a685..8edf56df9 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -6,6 +6,7 @@ site_description: Your first book to learn Data Structure And Algorithm. # Repository repo_name: krahets/hello-algo repo_url: https://github.com/krahets/hello-algo +edit_uri: https://github.com/krahets/hello-algo/master/docs/ # Copyright copyright: Copyright © 2020 - 2022 Krahets @@ -14,6 +15,7 @@ copyright: Copyright © 2020 - 2022 Krahets theme: name: material custom_dir: overrides + language: zh features: - announce.dismiss - content.code.annotate @@ -52,8 +54,9 @@ theme: code: Roboto Mono favicon: assets/images/favicon.png logo: assets/images/logo.png - # icon: - # logo: logo + icon: + logo: logo + repo: fontawesome/brands/github extra: social: @@ -61,7 +64,9 @@ extra: link: https://github.com/krahets - icon: fontawesome/brands/twitter link: https://twitter.com/krahets - generator: false + - icon: fontawesome/solid/code + link: https://leetcode.cn/u/jyd/ + # generator: false # Plugins plugins: @@ -113,12 +118,11 @@ extra_css: # Page tree nav: - - 前言: + - 关于本书: - chapter_introduction/index.md - - 如何使用本书: - - 算法学习建议: chapter_prerequisites/suggestions.md - - 编程环境安装: chapter_prerequisites/installation.md - - 一起参与创作: chapter_prerequisites/contribution.md + - 如何使用本书: chapter_introduction/suggestions.md + - 编程环境安装: chapter_introduction/installation.md + - 一起参与创作: chapter_introduction/contribution.md - 算法是什么: - chapter_dsa_introduction/index.md - 计算复杂度: @@ -151,5 +155,10 @@ nav: - 二分查找: chapter_searching/binary_search.md - 哈希查找: chapter_searching/hashing_search.md - 小结: chapter_searching/summary.md + - 排序算法: + - 冒泡排序: chapter_sorting/bubble_sort.md + - 插入排序: chapter_sorting/insertion_sort.md + - 归并排序: chapter_sorting/merge_sort.md + - 快速排序: chapter_sorting/quick_sort.md - 参考文献: - - chapter_reference/index.md \ No newline at end of file + - chapter_reference/index.md