Add bubble sort, insertion sort.
4
.gitignore
vendored
|
@ -5,8 +5,10 @@
|
|||
.vscode/
|
||||
|
||||
# mkdocs files
|
||||
overrides/
|
||||
site/
|
||||
.cache/
|
||||
overrides/
|
||||
codes/python
|
||||
codes/cpp
|
||||
|
||||
docs/chapter_*
|
21
README.md
|
@ -1,20 +1,10 @@
|
|||
# Welcome to MkDocs
|
||||
# Hello 算法
|
||||
|
||||
For full documentation visit [mkdocs.org](https://www.mkdocs.org).
|
||||
[<img src="docs/index.assets/conceptual_rendering.png" height="200" />](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 |
|
||||
| 更新:关于本书</br>新增:如何使用本书</br>新增:一起参与创作 | 2022-11-16 |
|
||||
| 新增:查找算法 | 2022-11-19 |
|
||||
| 更新:Markdown Stylesheet</br>新增:冒泡排序、插入排序 | 2022-11-21 |
|
||||
|
||||
## License
|
||||
|
||||
|
|
50
codes/java/chapter_sorting/bubble_sort.java
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
25
codes/java/chapter_sorting/insertion_sort.java
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
Before Width: | Height: | Size: 503 KiB |
BIN
docs/index.assets/animation.gif
Normal file
After Width: | Height: | Size: 127 KiB |
Before Width: | Height: | Size: 626 KiB After Width: | Height: | Size: 172 KiB |
Before Width: | Height: | Size: 276 KiB |
BIN
docs/index.assets/conceptual_rendering.png
Normal file
After Width: | Height: | Size: 217 KiB |
Before Width: | Height: | Size: 490 KiB After Width: | Height: | Size: 318 KiB |
|
@ -7,34 +7,38 @@ hide:
|
|||
=== " "
|
||||
|
||||
<div class="result" markdown>
|
||||
![conceptual_rendering](index.assets/conceptual_rendering.jpg){ align=left width=300 }
|
||||
</br>
|
||||
![conceptual_rendering](index.assets/conceptual_rendering.png){ align=left width=300 }
|
||||
</br>
|
||||
</br>
|
||||
</br>
|
||||
<h1 style="text-align:center"> 《 Hello,算法 》 </h1>
|
||||
<h3 style="text-align:center"> 动画图解、能运行、可讨论的</br>数据结构与算法快速入门教程 </h3>
|
||||
<h3 style="text-align:center"> [![github-stars](https://img.shields.io/github/stars/krahets/hello-algo?style=social)](https://github.com/krahets/hello-algo) </h3>
|
||||
<p style="text-align:center"> [@Krahets](https://leetcode.cn/u/jyd/) </p>
|
||||
<p style="text-align:center"> [![github-stars](https://img.shields.io/github/stars/krahets/hello-algo?style=social)](https://github.com/krahets/hello-algo) </p>
|
||||
</br>
|
||||
</div>
|
||||
|
||||
---
|
||||
|
||||
<h2 style="text-align:center"> 「动画 + 图片 + HTML」 </h2>
|
||||
<h2 style="text-align:center"> 「清晰动画讲解」 </h2>
|
||||
|
||||
<p style="text-align:center"> 重点知识以动画和图解为主,提升知识吸收效率</br>由 MkDocs 构建文档,支持笔记本、平板、手机多种终端 </p>
|
||||
<p style="text-align:center"> 借助动画介绍重点,提升知识吸收效率</br>HTML 文档,支持笔记本、平板、手机多种终端 </p>
|
||||
|
||||
![algorithm_animation](index.assets/algorithm_animation.gif)
|
||||
![algorithm_animation](index.assets/animation.gif)
|
||||
|
||||
<h2 style="text-align:center"> 「面向代码,注重实践」 </h2>
|
||||
---
|
||||
|
||||
<h2 style="text-align:center"> 「代码实践导向」 </h2>
|
||||
|
||||
<p style="text-align:center"> 示例代码皆可一键运行,在调试中加深理解</br>提供 Java, C++, Python 源码与详细注释 </p>
|
||||
|
||||
![running_code](index.assets/running_code.gif)
|
||||
|
||||
<h2 style="text-align:center"> 「可讨论,能提问」 </h2>
|
||||
---
|
||||
|
||||
<p style="text-align:center"> 在评论区和小伙伴们一起讨论进步</br>作者定期回复评论问题(一般 < 72h ) </p>
|
||||
<h2 style="text-align:center"> 「可讨论与提问」 </h2>
|
||||
|
||||
<p style="text-align:center"> 在评论区与小伙伴们一起学习进步</br>作者定期回复评论问题(一般 < 72h ) </p>
|
||||
|
||||
![comment](index.assets/comment.gif)
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
27
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
|
||||
- chapter_reference/index.md
|
||||
|
|