diff --git a/notes/算法.md b/notes/算法.md index 079a53e0..fa740d34 100644 --- a/notes/算法.md +++ b/notes/算法.md @@ -354,21 +354,21 @@ public class UF { 但是 union 操作代价却很高,需要将其中一个连通分量中的所有节点 id 值都修改为另一个节点的 id 值。 -

+

```java - public int find(int p) { - return id[p]; - } - public void union(int p, int q) { - int pID = find(p); - int qID = find(q); +public int find(int p) { + return id[p]; +} +public void union(int p, int q) { + int pID = find(p); + int qID = find(q); - if (pID == qID) return; - for (int i = 0; i < id.length; i++) { - if (id[i] == pID) id[i] = qID; - } + if (pID == qID) return; + for (int i = 0; i < id.length; i++) { + if (id[i] == pID) id[i] = qID; } +} ``` ## quick-union @@ -377,7 +377,7 @@ public class UF { 但是 find 操作开销很大,因为同一个连通分量的节点 id 值不同,id 值只是用来指向另一个节点。因此需要一直向上查找操作,直到找到最上层的节点。 -

+

```java public int find(int p) { @@ -563,12 +563,12 @@ public class Shell {

-

- ### 1. 归并方法 归并方法将数组中两个已经排序的部分归并成一个。 +

+ ```java public class MergeSort { private static Comparable[] aux; diff --git a/pics/9764bf07-4840-486c-988f-334acefc3f49.png b/pics/9764bf07-4840-486c-988f-334acefc3f49.png new file mode 100644 index 00000000..0c99b1cb Binary files /dev/null and b/pics/9764bf07-4840-486c-988f-334acefc3f49.png differ diff --git a/pics/c5043b7e-6efe-4c55-b663-272e1097ed21.png b/pics/c5043b7e-6efe-4c55-b663-272e1097ed21.png new file mode 100644 index 00000000..03a6c34f Binary files /dev/null and b/pics/c5043b7e-6efe-4c55-b663-272e1097ed21.png differ