diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_1.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_1.png new file mode 100644 index 000000000..a283003fa Binary files /dev/null and b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_1.png differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_2.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_2.png new file mode 100644 index 000000000..88f66ec4b Binary files /dev/null and b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_2.png differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_3.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_3.png new file mode 100644 index 000000000..900492f1c Binary files /dev/null and b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_3.png differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_4.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_4.png new file mode 100644 index 000000000..0220670de Binary files /dev/null and b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_4.png differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_5.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_5.png new file mode 100644 index 000000000..7459d186a Binary files /dev/null and b/docs/chapter_introduction/algorithms_are_everywhere.assets/binary_search_dictionary_step_5.png differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/greedy_change.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/greedy_change.png new file mode 100644 index 000000000..5ab4f3237 Binary files /dev/null and b/docs/chapter_introduction/algorithms_are_everywhere.assets/greedy_change.png differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_1.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_1.png deleted file mode 100644 index 9eda07d77..000000000 Binary files a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_1.png and /dev/null differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_2.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_2.png deleted file mode 100644 index e08403872..000000000 Binary files a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_2.png and /dev/null differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_3.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_3.png deleted file mode 100644 index 2c0459f89..000000000 Binary files a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_3.png and /dev/null differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_4.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_4.png deleted file mode 100644 index 45f9eb2b1..000000000 Binary files a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_4.png and /dev/null differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_5.png b/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_5.png deleted file mode 100644 index 95cd445c2..000000000 Binary files a/docs/chapter_introduction/algorithms_are_everywhere.assets/look_up_dictionary_step_5.png and /dev/null differ diff --git a/docs/chapter_introduction/algorithms_are_everywhere.md b/docs/chapter_introduction/algorithms_are_everywhere.md index bf2b31562..dafdb397e 100644 --- a/docs/chapter_introduction/algorithms_are_everywhere.md +++ b/docs/chapter_introduction/algorithms_are_everywhere.md @@ -4,7 +4,7 @@ 在正式探讨算法之前,有一个有趣的事实值得分享:**实际上,你已经学会了许多算法,并习惯将他们应用到日常生活中了**。下面,我将举两个具体例子来证实这一点。 -**例一:组装积木**。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。 +**例一:拼装积木**。一套积木,除了包含许多零件之外,还附有详细的组装说明书。我们按照说明书一步步操作,就能组装出精美的积木模型。 从数据结构与算法的角度来看,积木的各种形状和连接方式代表数据结构,而组装说明书上的一系列步骤则是算法。 @@ -17,19 +17,19 @@ 3. 不断重复步骤 1-2 ,直至找到拼音首字母为 $r$ 的页码为止。 === "<1>" - ![查字典步骤](algorithms_are_everywhere.assets/look_up_dictionary_step_1.png) + ![查字典步骤](algorithms_are_everywhere.assets/binary_search_dictionary_step_1.png) === "<2>" - ![look_up_dictionary_step_2](algorithms_are_everywhere.assets/look_up_dictionary_step_2.png) + ![binary_search_dictionary_step_2](algorithms_are_everywhere.assets/binary_search_dictionary_step_2.png) === "<3>" - ![look_up_dictionary_step_3](algorithms_are_everywhere.assets/look_up_dictionary_step_3.png) + ![binary_search_dictionary_step_3](algorithms_are_everywhere.assets/binary_search_dictionary_step_3.png) === "<4>" - ![look_up_dictionary_step_4](algorithms_are_everywhere.assets/look_up_dictionary_step_4.png) + ![binary_search_dictionary_step_4](algorithms_are_everywhere.assets/binary_search_dictionary_step_4.png) === "<5>" - ![look_up_dictionary_step_5](algorithms_are_everywhere.assets/look_up_dictionary_step_5.png) + ![binary_search_dictionary_step_5](algorithms_are_everywhere.assets/binary_search_dictionary_step_5.png) 查阅字典这个小学生必备技能,实际上就是著名的「二分查找」。从数据结构的角度,我们可以把字典视为一个已排序的「数组」;从算法的角度,我们可以将上述查字典的一系列操作看作是「二分查找」算法。 @@ -38,9 +38,12 @@ 1. 可选项是比 $31$ 元面值更小的货币,包括 $1$ , $5$ , $10$ , $20$ 元。 2. 从可选项中拿出最大的 $20$ 元,剩余 $31 - 20 = 11$ 元。 3. 从剩余可选项中拿出最大的 $10$ 元,剩余 $11 - 10 = 1$ 元。 -4. 从剩余可选项中拿出最大的 $1$ 元,剩余 $1 - 1 = 0$ 元。完成找零。 +4. 从剩余可选项中拿出最大的 $1$ 元,剩余 $1 - 1 = 0$ 元。 +5. 完成找零,方案为 $20 + 10 + 1 = 31$ 元。 -在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案 $20 + 10 + 1 = 31$ 元。从数据结构与算法的角度看,以上方法本质上是「贪心算法」。 +在以上步骤中,我们每一步都采取当前看来最好的选择(尽可能用大面额的货币),最终得到了可行的找零方案。从数据结构与算法的角度看,这种方法本质上是「贪心算法」。 + +![货币找零](algorithms_are_everywhere.assets/greedy_change.png) 小到烹饪一道菜,大到星际航行,几乎所有问题的解决都离不开算法。计算机的出现使我们能够通过编程将数据结构存储在内存中,同时编写代码调用 CPU 和 GPU 执行算法。这样一来,我们就能把生活中的问题转移到计算机上,以更高效的方式解决各种复杂问题。