├── .classpath
├── .gitignore
├── README.md
└── src
└── me
└── zonglun
├── AutumnRecruitment
└── FireHome
│ ├── Soldierqueue.java
│ ├── XiaoMingsChessboard.java
│ ├── coding2019
│ ├── makeFriends.java
│ └── samleString.java
│ └── learningSpelling.java
├── BinaryTree
├── BST.java
├── BinarySearch.java
├── BinarySearch2.java
├── BlackRedBST.java
├── FileOperations.java
├── Main.java
├── SST.java
├── SeparateChainingHashST.java
├── SequentialSearchST.java
├── TestBST.java
├── TestBinarySearch.java
├── TestRedBlackBST.java
└── bible.txt
├── Graph
├── Components.java
├── DenseGraph.java
├── Edge.java
├── Graph.java
├── Path.java
├── ReadGraph.java
├── ReadWeightedGraph.java
├── ShortestPath.java
├── SparseGraph.java
├── SparseWeightedGraph.java
├── TestGraph.java
├── TestPath.java
├── TestWeightedGraph.java
├── WeightedGraph.java
├── testG.txt
├── testG1.txt
└── testG2.txt
├── Sort
├── BubbleSort.java
├── GetThNumber.java
├── HeapSort1.java
├── HeapSort2.java
├── HeapSort3.java
├── IndexHeapSort.java
├── IndexMaxHeap.java
├── InsertionSort.java
├── InversionPair.java
├── MaxHeap.java
├── MergeSort.java
├── MergeSortBU.java
├── PrintableMaxHeap.java
├── QuickSort.java
├── QuickSort2Ways.java
├── QuickSortOptimize.java
├── QuickSortThreeWays.java
├── README.md
├── RadixSort.java
├── SelectionSort.java
├── ShellSort.java
├── SortTestHelper.java
└── Student.java
├── SwordToOffer
├── AddFunction.java
├── ConvertBSTToLinkList.java
├── DeleteDuplicationListNode.java
├── DoublylinkedList.java
├── DuplicateNum.java
├── EntryNodeOfLoop.java
├── Fibonacci.java
├── FindContinuousSequence.java
├── FindFirstCommonNodeInList.java
├── FindGreatestSumOfSubArray.java
├── FindInPartiallySortedMatrix.java
├── FindKthInBinaryTree.java
├── FindKthToTail.java
├── FindNumbersWithSum.java
├── FindNumsAppearOnce.java
├── FindPathInBinaryTree.java
├── FirstAppearingOnce.java
├── FirstNotRepeatingChar.java
├── FlowWindows.java
├── GetLeastNumbers_Solution.java
├── GetMinStack.java
├── GetNextTreeNode.java
├── GetNumOfK.java
├── GetUglyNumber_Solution.java
├── HasSubtree.java
├── InversePairs.java
├── IsBalanced_Solution.java
├── IsContinuous.java
├── IsPopOrder.java
├── IsSymmetrical.java
├── JumpSteps.java
├── JumpStepsII.java
├── LastRemaining_Solution.java
├── LeftRotateString.java
├── ListNodeMerge.java
├── MatrxPath.java
├── MiddleNumberInDataFlow.java
├── MinNumberInRotateArray.java
├── MirrorBinaryTree.java
├── MoreThanHalfNum_Solution.java
├── MoveMachine.java
├── MultiplyArray.java
├── NextTreeNode.java
├── NumberOf1.java
├── NumberOf1Between1AndN_Solution.java
├── PermutationList.java
├── Power.java
├── PrintBinaryTreeByZ.java
├── PrintBinayTreeInOrder.java
├── PrintFromTopToBottom.java
├── PrintListFromTailToHead.java
├── PrintMatrix.java
├── PrintMinNumber.java
├── RandomListNodeClone.java
├── ReConstructBinaryTree.java
├── ReOrderArray.java
├── RectCover.java
├── RegMach.java
├── ReplaceBlank.java
├── ReverseList.java
├── ReverseSentence.java
├── SerializeBinaryTree.java
├── StrToInt.java
├── StringIsNum.java
├── Sum_SolutionN.java
├── TreeDepth.java
├── TwoStackQueue.java
├── VerifySquenceOfBST.java
└── util
│ ├── ListNode.java
│ ├── TreeLinkNode.java
│ └── TreeNode.java
├── UnionFind
├── UnionFind1.java
├── UnionFind2.java
├── UnionFind3.java
├── UnionFind4.java
└── UnionFindTestHelper.java
└── leetcode
├── Algorithms
├── CardinalitySort.java
├── DP
│ ├── Packages01.java
│ ├── question322
│ │ └── coinChange.java
│ ├── question53
│ │ └── MaximumSubarray.java
│ └── question70
│ │ └── ClimbingStairs.java
└── greedy
│ ├── Test.java
│ ├── question135
│ ├── PuKePai.java
│ └── candy.java
│ ├── question376
│ └── wiggleSubsequence.java
│ └── question455
│ └── assigncookies.java
├── FibooCiles.java
├── MinStringSum.java
├── QueueAndStack
├── BFS.java
└── question200
│ └── numberOfLslands.java
├── RundomNumArray.java
├── arrayAndString
├── Main.java
└── question1
│ └── TwoSum.java
├── linkedList
├── MyLinkedList.java
├── question138
│ └── copylistwithrandompointer.java
├── question141
│ └── shileCleIs.java
├── question142
│ └── linkedlistcycleii.java
├── question160
│ └── IntersectionOfTwoLinkedLists.java
├── question19
│ └── removenthnodefromendoflist.java
├── question203
│ └── removelinkedlistelements.java
├── question206
│ └── reverseLinkedList.java
├── question21
│ └── mergeTwoSortedLists.java
├── question23
│ └── mergeksortedlists.java
├── question234
│ └── palindromelinkedlist.java
├── question86
│ └── partitionList.java
└── question92
│ └── reverseLinkedList2.java
└── utils
├── RandomListNode.java
└── StopWatch.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .settings/
2 | bin/
3 | *.class
4 | .project
5 | .idea/
6 | CoderEnterView.eml
7 | CoderEnterView.iml
8 | AlgorithmsSolutions.iml
9 | out/
10 |
--------------------------------------------------------------------------------
/src/me/zonglun/AutumnRecruitment/FireHome/Soldierqueue.java:
--------------------------------------------------------------------------------
1 | package me.zonglun.AutumnRecruitment.FireHome;
2 |
3 | /**
4 | * 士兵队列
5 | *
6 | * @author : Administrator
7 | * @create 2018-09-09 15:26
8 | * @subject :
9 | * 一队士兵在操场上排成一列,士兵总数为n,每个士兵有各自的身高。
10 | *
11 | * 士兵列队完毕后,将军走到队列的最前面。因为身高不一,有些士兵可能被前面身高更高的挡住了,这样将军就看不到他们。
12 | * 将军能看到某个士兵当且仅当他的身高严格大于他前面的所有士兵。
13 | *
14 | * 将军发现这些士兵的身高为a1,a2,a3,...,an。
15 | * 而且他恰好能看到m个士兵。将军想知道一共有多少种可能的站队方式。两种站队方式不同当且仅当至少存在一个士兵,他站在了队列的不同位置。最后方案数可能很大,输出方案数除以1000000007后的余数。
16 | *
17 | *
18 | * 输入
19 | * 输入只有两行,第一行两个数n和m,(1<=m<=n<=1000)第二行有n个数,a1到an。(1<=ai<=1000000000)
20 | * 样例输入
21 | * 4 3
22 | *
23 | * 1 1 2 3
24 | * 输出
25 | * 输出一行,问题的答案。
26 | * 样例输出
27 | * 6
28 | */
29 | public class Soldierqueue {
30 |
31 | public static void main(String[] args) {
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/me/zonglun/AutumnRecruitment/FireHome/XiaoMingsChessboard.java:
--------------------------------------------------------------------------------
1 | package me.zonglun.AutumnRecruitment.FireHome;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * 小明的棋盘
7 | *
8 | * @author : Administrator
9 | * @create 2018-09-08 20:23
10 | * @subject :
11 | *
12 | * 题目描述
13 | *
14 | * 小明有天看到一个放满黑子和白子交替的大小为n*m的棋盘。然而小明只喜欢只有一种颜色的棋盘,即只有黑子或者白子。小明拥有超能力可以将一个里面的矩阵的棋子的颜色反转,但是他一天最多只能使用c次超能力。如果可以将所有棋子变成一种颜色,小明就会买这个棋盘,请问小明是否会买这个棋盘。
15 | *
16 | * 输入
17 | * 第一行一个整数 T(T≤10) 表示数据组数。
18 | *
19 | * 每组数据有一行, 三个正整数 n,m,c(1≤n,m,c≤1e9)。
20 | *
21 | *
22 | *
23 | * 样例输入
24 | * 2
25 | *
26 | * 1 2 1
27 | *
28 | * 2 2 1
29 | *
30 | * 输出
31 | * 如果小明买这个棋盘输出 "Yes" 否则输出 "No".
32 | *
33 | *
34 | *
35 | * 样例输出
36 | *
37 | * Yes
38 | * No
39 | *
40 | */
41 | public class XiaoMingsChessboard {
42 | public static void main(String[] args) {
43 | Scanner scan = new Scanner(System.in);
44 | while (scan.hasNext()) {
45 | int t = scan.nextInt();
46 | for (int i = 0; i < t; i++) {
47 | long n = scan.nextInt();
48 | long m = scan.nextInt();
49 | long c = scan.nextInt();
50 | if (n /2 + m / 2 <= c) {
51 | System.out.println("Yes");
52 | } else {
53 | System.out.println("No");
54 | }
55 | }
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/me/zonglun/AutumnRecruitment/FireHome/coding2019/makeFriends.java:
--------------------------------------------------------------------------------
1 | package me.zonglun.AutumnRecruitment.FireHome.coding2019;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * 交友大会
7 | *
8 | * @author : Administrator
9 | * @create 2018-09-17 21:39
10 | * @subject :
11 | *
12 | *
13 | */
14 | public class makeFriends {
15 | public static void main(String[] args) {
16 | Scanner sc = new Scanner(System.in);
17 | while (sc.hasNext()) {
18 | int n = sc.nextInt();
19 | for (int i = 0; i < n; i++) {
20 | int m = sc.nextInt();
21 | int k = sc.nextInt();
22 | System.out.println(getN(m,k));
23 | }
24 | }
25 | }
26 |
27 | private static int getN(int n, int m) {
28 | if (1 == n || 1 == m) {
29 | return 1;
30 | }
31 | if (n <= m) {
32 | return getN(n, n - 1) + 1;
33 |
34 | } else {
35 | return getN(n, m - 1) + getN(n - m, m);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/me/zonglun/AutumnRecruitment/FireHome/coding2019/samleString.java:
--------------------------------------------------------------------------------
1 | package me.zonglun.AutumnRecruitment.FireHome.coding2019;
2 |
3 | import java.util.HashMap;
4 | import java.util.Scanner;
5 |
6 | /**
7 | * @author : Administrator
8 | * @create 2018-09-17 21:41
9 | * @subject :
10 | * 给出两个字符串a1a2a3...an和b1b2b3...bm,
11 | * 如果存在一种映射ai=f(bi),且如果ai≠bi,f(ai)≠f(bi),我们称这两个字符串同构。
12 | *
13 | * 输入
14 | * 输入第一行包含一个t,表示数据组数,t<=20.
15 | *
16 | * 对于每组数据,输入包含两行,每一行有一个字符串。
17 | * * 每个字符串的长度不超过10000,字符串全部由小写字母组成。
18 | * * 输出
19 | *
20 | * 对于每组数据输出一行,如果字符串同构,输出“Yes”,不同构则输出“No”。
21 | *
22 | 样例输入
23 | 2
24 | abac
25 | bcbd
26 | aba
27 | abc
28 | 样例输出
29 | Yes
30 | No
31 | *
32 | */
33 | public class samleString {
34 | public static void main(String[] args) {
35 | Scanner sc = new Scanner(System.in);
36 | while (sc.hasNext()) {
37 | int n = sc.nextInt();
38 | for (int j = 0; j < n; j++) {
39 | String s1 = sc.nextLine();
40 | String s2 = sc.nextLine();
41 | HashMap map = new HashMap<>();
42 | for (int i = 0; i < s1.length(); i++) {
43 | char a = s1.charAt(i);
44 | char b = s2.charAt(i);
45 | if (map.containsKey(a)) {
46 | if (map.get(a).equals(b)) {
47 | continue;
48 | } else {
49 | System.out.println("No");
50 | }
51 | } else {
52 | if (!map.containsValue(b)) {
53 | map.put(a, b);
54 | } else {
55 | System.out.println("No");
56 | }
57 | }
58 | }
59 | System.out.println("Yes");
60 | }
61 | }
62 | }
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/src/me/zonglun/AutumnRecruitment/FireHome/learningSpelling.java:
--------------------------------------------------------------------------------
1 | package me.zonglun.AutumnRecruitment.FireHome;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * 学打字
7 | *
8 | * @author : Administrator
9 | * @create 2018-09-08 21:07
10 | * @subject :
11 | * 小明同学刚刚学习打字,现在老师叫他输入一个英文字符串。小明发现,这个英文字符串只由大写和小写的英文字母构成。小明同学只会使用Caps Lock键来切换大小写输入。
12 | *
13 | * 最开始,大写锁定处于关闭状态,小明的电脑只能输入小写英文字母。当大写锁定关闭时,按一下Caps Lock键可以打开大写锁定,之后只能输入大写字母;当大写锁定打开时,按一下Caps Lock键可以关闭大写锁定,之后只能输入小写字母。
14 | *
15 | * 现在小明想知道输入这个字符串最少需要按键多少次。
16 | *
17 | * 输入
18 | * 输入只有一行,一个字符串,字符串中只有大写字母和小写字母,字符串长度不超过100000。
19 | * 样例输入
20 | * aAAbB
21 | * 输出
22 | * 输出一行,最少的按键次数。
23 | * 案例输出为8
24 | */
25 | public class learningSpelling {
26 |
27 | public static void main(String[] args) {
28 | Scanner sc = new Scanner(System.in);
29 | String str = sc.next();
30 | char[] chars = str.toCharArray();
31 | int sumNum = 0;
32 | if (Character.isUpperCase(chars[0])) {
33 | sumNum = 1;
34 | } else {
35 | sumNum = 0;
36 | }
37 | for (int i = 0; i < chars.length -1; i++) {
38 | int j = i + 1;
39 | char c = chars[i], d = chars[j];
40 | if (Character.isUpperCase(c) ^ Character.isUpperCase(d)) {
41 | sumNum ++;
42 | }
43 | }
44 | System.out.println(sumNum + chars.length);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/me/zonglun/BinaryTree/BinarySearch.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: BinarySearch
4 | * Author: Administrator
5 | * Date: 2017/11/7 0007 21:30
6 | * Description: BinarySearch 二分查找算法
7 | */
8 |
9 | package me.zonglun.BinaryTree;
10 |
11 | import java.util.stream.IntStream;
12 |
13 | /**
14 | * 〈一句话功能简述〉
15 | * 〈BinarySearch 二分查找算法〉
16 | *
17 | * @author Administrator
18 | * @create 2017/11/7 0007
19 | * @since 1.0.0
20 | */
21 | public class BinarySearch {
22 | private BinarySearch() {
23 | }
24 |
25 | // 二分查找法,在有序数组arr中,查找target (值不是索引)
26 | // 如果找到target,返回相应的索引index
27 | // 如果没有找到target,返回-1
28 | public static int find(Comparable[] arr, Comparable target) {
29 | // 在arr[l...r]之中查找target
30 | int l = 0, r = arr.length - 1;
31 | while (l <= r) { // l 和 r 为索引意味着当 l = r 的时候说明这时候
32 | // int mid = (l + r) / 2;
33 | int mid = l + (r - l) / 2;
34 | // 防止极端情况下的整形溢出,使用下面的逻辑求出mid
35 | if (arr[mid].compareTo(target) == 0)
36 | return mid;
37 | // 说明 target 在 mid 的左半部分, 此时在arr【l, mid - 1】之中再次循环
38 | if (arr[mid].compareTo(target) > 0)
39 | r = mid - 1;
40 | else // 说明 target 在 mid 的右半部分 ,此时在arr【mid + 1,r】 之中再次循环
41 | l = mid + 1;
42 | }
43 | return -1;
44 | }
45 |
46 | // 测试非递归的二分查找算法
47 | public static void main(String[] args) {
48 |
49 | int N = 1000000;
50 | Integer[] arr = new Integer[N];
51 | for (int i = 0; i < N; i++)
52 | arr[i] = i;
53 | // 对于我们的待查找数组[0...N)
54 | // 对[0...N)区间的数值使用二分查找,最终结果应该就是数字本身
55 | // 对[N...2*N)区间的数值使用二分查找,因为这些数字不在arr中,结果为-1
56 | IntStream.range(0, 2 * N).forEach(i -> {
57 | int v = BinarySearch.find(arr, i);
58 | if (i < N)
59 | assert v == i;
60 | else
61 | assert v == -1;
62 | });
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/me/zonglun/BinaryTree/BinarySearch2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: BinarySearch2
4 | * Author: Administrator
5 | * Date: 2017/11/7 0007 21:51
6 | * Description: 递归版本的二分查找算法
7 | */
8 |
9 | package me.zonglun.BinaryTree;
10 |
11 | import me.zonglun.Sort.InsertionSort;
12 |
13 | /**
14 | * 〈一句话功能简述〉
15 | * 〈递归版本的二分查找算法〉
16 | *
17 | * @author Administrator
18 | * @create 2017/11/7 0007
19 | * @since 1.0.0
20 | */
21 | public class BinarySearch2 {
22 | private BinarySearch2() {
23 | }
24 |
25 | private static int find(Comparable[] arr, int l, int r, Comparable target) {
26 | if (l > r)
27 | return -1;
28 | int mid = l + (r - l) / 2;
29 | if (arr[mid].compareTo(target) == 0)
30 | return mid;
31 | else if (arr[mid].compareTo(target) < 0)
32 | return find(arr, mid + 1, r, target);
33 | else
34 | return find(arr, l, mid - 1, target);
35 | }
36 |
37 | public static int find(Comparable[] arr, Comparable target) {
38 | return find(arr, 0, arr.length - 1, target);
39 | }
40 |
41 | public static void main(String[] args) {
42 | Comparable[] arr = {4, 6, 2, 1, 7, 12, 78, 94, 123, 11};
43 | InsertionSort.sort(arr);
44 | System.out.println(BinarySearch2.find(arr, 11));
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/me/zonglun/BinaryTree/SeparateChainingHashST.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: SeparateChainingHashST
4 | * Author: Administrator
5 | * Date: 2017/11/12 0012 16:47
6 | * Description: 基于拉链法的散列表
7 | */
8 |
9 | package me.zonglun.BinaryTree;
10 |
11 |
12 | /**
13 | * 〈一句话功能简述〉
14 | * 〈基于拉链法的散列表〉
15 | *
16 | * @author Administrator
17 | * @create 2017/11/12 0012
18 | * @since 1.0.0
19 | */
20 | public class SeparateChainingHashST {
21 | private int N; // 键值对总数
22 | private int M; // 散列表大小
23 | private SequentialSearchST[] st; // 存放链表对象的数组
24 |
25 | public SeparateChainingHashST() {
26 | this(997);
27 | }
28 |
29 | private SeparateChainingHashST(int M) {
30 | // 创建M 条链表
31 | this.M = M;
32 | st = (SequentialSearchST[]) new SequentialSearchST[M];
33 | for (int i = 0; i < M; i++) {
34 | st[i] = new SequentialSearchST();
35 | }
36 | }
37 | /*
38 | 散列表的hash 函数
39 | * */
40 | private int hash(Key key) {
41 | return (key.hashCode() & 0x7fffffff) % M;
42 | }
43 |
44 | public Value get(Key key) {
45 | return (Value) st[hash(key)].get(key);
46 | }
47 |
48 | public void put(Key key, Value val) {
49 | st[hash(key)].put(key, val);
50 | }
51 |
52 | public static void main(String[] args) {
53 | SeparateChainingHashST hashTable = new SeparateChainingHashST<>();
54 | int N = 100;
55 | int M = 10000;
56 | for (int i = 0; i < N; i++) {
57 | Integer key = (int) (Math.random() * M);
58 | Integer value = (int)(Math.random() * M);
59 | hashTable.put(key, value);
60 | hashTable.put(345,345900);
61 | long startTime = System.currentTimeMillis();
62 | int result = hashTable.get(345);
63 | long endTime = System.currentTimeMillis();
64 | long time = endTime - startTime;
65 | System.out.println("the time is " + time + "result is " + result);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/me/zonglun/BinaryTree/SequentialSearchST.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: SequentialSearchST
4 | * Author: Administrator
5 | * Date: 2017/11/12 0012 16:51
6 | * Description: 链式查找链表的显示
7 | */
8 |
9 | package me.zonglun.BinaryTree;
10 |
11 |
12 | import sun.misc.Queue;
13 |
14 | /**
15 | * 〈一句话功能简述〉
16 | * 〈链式查找链表的显示〉
17 | *
18 | * @author Administrator
19 | * @create 2017/11/12 0012
20 | * @since 1.0.0
21 | */
22 | public class SequentialSearchST {
23 | private Node first;
24 | private int N = 0;
25 |
26 | private class Node {
27 | Key key;
28 | Value val;
29 | Node next;
30 |
31 | public Node(Key key, Value val, Node next) {
32 | this.key = key;
33 | this.val = val;
34 | this.next = next;
35 | }
36 | }
37 |
38 | public Value get(Key key) {
39 | for (Node x = first; x != null; x = x.next) {
40 | if (key.equals(x.key)) {
41 | return x.val;
42 | }
43 | }
44 | return null;
45 | }
46 |
47 | public void put(Key key, Value val) {
48 | for (Node x = first; x != null; x = x.next) {
49 | if (key.equals(x.key)) {
50 | x.val = val;
51 | return;
52 | }
53 | }
54 | first = new Node(key, val, first);
55 | N++;
56 | }
57 |
58 |
59 | public int size() {
60 | return N;
61 | }
62 |
63 | public void delete(Key key) {
64 | first = delete(first, key);
65 | }
66 |
67 | private Node delete(Node x, Key key) {
68 | if (x == null) {
69 | return null;
70 | }
71 | if (x.key.equals(key)) {
72 | N--;
73 | return x.next;
74 | }
75 | x.next = delete(x.next, key);
76 | return x;
77 | }
78 |
79 | public Iterable keys() {
80 | Queue queue = new Queue<>();
81 | for (Node x = first; x != null; x = x.next) {
82 | queue.enqueue(x.key);
83 | }
84 | return (Iterable) queue;
85 | }
86 | }
87 |
88 |
--------------------------------------------------------------------------------
/src/me/zonglun/BinaryTree/TestBinarySearch.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: TestBinarySearch
4 | * Author: Administrator
5 | * Date: 2017/11/8 0008 10:38
6 | * Description: 比较非递归和递归写法的二分查找的效率
7 | */
8 |
9 | package me.zonglun.BinaryTree;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈 比较非递归和递归写法的二分查找的效率〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/8 0008
17 | * @since 1.0.0
18 | */
19 | public class TestBinarySearch {
20 |
21 | private TestBinarySearch() {
22 | }
23 |
24 | public static void main(String[] args) {
25 |
26 | int N = 1000000;
27 | Integer[] arr = new Integer[N];
28 | for (int i = 0; i < N; i++)
29 | arr[i] = i;
30 |
31 |
32 | // 测试非递归二分查找法
33 | long startTime = System.currentTimeMillis();
34 |
35 | // 对于我们的待查找数组[0...N)
36 | // 对[0...N)区间的数值使用二分查找,最终结果应该就是数字本身
37 | // 对[N...2*N)区间的数值使用二分查找,因为这些数字不在arr中,结果为-1
38 | for (int i = 0; i < 2 * N; i++) {
39 | int v = BinarySearch.find(arr, i);
40 | if (i < N)
41 | assert v == i;
42 | else
43 | assert v == -1;
44 | }
45 | long endTime = System.currentTimeMillis();
46 |
47 | System.out.println("Binary Search (Without Recursion): " + (endTime - startTime) + "ms");
48 |
49 |
50 | // 测试递归的二分查找法
51 | startTime = System.currentTimeMillis();
52 |
53 | // 对于我们的待查找数组[0...N)
54 | // 对[0...N)区间的数值使用二分查找,最终结果应该就是数字本身
55 | // 对[N...2*N)区间的数值使用二分查找,因为这些数字不在arr中,结果为-1
56 | for (int i = 0; i < 2 * N; i++) {
57 | int v = BinarySearch2.find(arr, i);
58 | if (i < N)
59 | assert v == i;
60 | else
61 | assert v == -1;
62 | }
63 | endTime = System.currentTimeMillis();
64 | System.out.println("Binary Search (With Recursion): " + (endTime - startTime) + "ms");
65 |
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/me/zonglun/BinaryTree/TestRedBlackBST.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: TesTRedBlackBST
4 | * Author: Administrator
5 | * Date: 2017/11/12 0012 16:17
6 | * Description: 测试红黑树
7 | */
8 |
9 | package me.zonglun.BinaryTree;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈测试红黑树〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/12 0012
17 | * @since 1.0.0
18 | */
19 | public class TestRedBlackBST {
20 | public static void main(String[] args) {
21 | BlackRedBST Rb = new BlackRedBST<>();
22 | int N = 10;
23 | int M = 100;
24 | for (int i = 0; i < N; i++) {
25 | Integer key = (int) (Math.random() * M);
26 | Rb.put(key, key);
27 | System.out.print(key + " ");
28 | }
29 | System.out.println();
30 |
31 | // 测试二分搜索树的size()
32 | System.out.println("size: " + Rb.size());
33 | System.out.println();
34 | System.out.println(Rb.max());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/Components.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: Components
4 | * Author: Administrator
5 | * Date: 2017/11/14 0014 10:53
6 | * Description: // 求无权图的联通分量
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈// 求无权图的联通分量〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/14 0014
17 | * @since 1.0.0
18 | */
19 | public class Components {
20 | Graph G; // 图的引用
21 | private boolean[] visited; // 记录dfs的过程中节点是否被访问
22 | private int ccount; // 记录联通分量个数
23 | private int[] id; // 每个节点所对应的联通分量标记,相链接的ID的大小值是相同的
24 |
25 | public Components(Graph graph) {
26 | G = graph;
27 | visited = new boolean[G.V()]; // visited 开辟新的空间,大小为图里面的节点个数
28 | id = new int[G.V()]; // 在 ID 里面开辟新的空间,大小为图里面的节点个数
29 | ccount = 0;
30 | for (int i = 0; i < G.V(); i++) {
31 | visited[i] = false; // 初始化应该节点都没有被访问过
32 | id[i] = -1; // 初始化的时候使得所有的元素的值为-1
33 | }
34 |
35 | for (int i = 0; i < G.V(); i++) {
36 | if (!visited[i]) {
37 | dfs(i); // 图的深度优先遍历-
38 | ccount++;
39 | }
40 | }
41 | }
42 |
43 | int getCount() {
44 | return ccount;
45 | }
46 |
47 | boolean isConnected(int v, int w) {
48 | assert v >= 0 && v < G.V();
49 | assert w >= 0 && w < G.V();
50 | return id[v] == id[w];
51 | }
52 |
53 | // 图的深度优先遍历----递归实现
54 | private void dfs(int v) {
55 | visited[v] = true;
56 | id[v] = ccount;
57 |
58 | for (int i : G.adj(v)) {
59 | if (!visited[i]) // 如果已经被访问过了则继续循环
60 | dfs(i); // 递归运行i
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/DenseGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: DenseGraph
4 | * Author: Administrator
5 | * Date: 2017/11/12 0012 21:42
6 | * Description: 稠密图Java展示
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | import java.util.Vector;
12 |
13 | /**
14 | * 〈一句话功能简述〉
15 | * 〈稠密图Java展示〉
16 | *
17 | * @author Administrator
18 | * @create 2017/11/12 0012
19 | * @since 1.0.0
20 | */
21 | public class DenseGraph implements Graph {
22 |
23 | private int n; // 节点数
24 | private int m; // 边数
25 | private boolean directed; // 是否为有向图
26 | // 边的链接情况--用相邻界矩阵来表示
27 | private boolean[][] g;
28 |
29 | public DenseGraph(int n, boolean directed) {
30 | assert n > 0;
31 | this.n = n;
32 | this.m = 0;
33 | this.directed = directed;
34 | // 刚开始是创建新的 n*n阶的矩阵,值都为false
35 | this.g = new boolean[n][n];
36 | }
37 |
38 | @Override
39 | public int V() {
40 | return n;
41 | } // 返回节点的个数
42 |
43 | @Override
44 | public int E() {
45 | return m;
46 | } // 返回图中边的个数
47 |
48 | @Override
49 | public void addEdge(int v, int w) {
50 | assert v >= 0 && v < n;
51 | assert w >= 0 && w < n;
52 | if (hasEdge(v, w)) // 判断是否图中已经有了相关的图,如有则返回
53 | return;
54 | g[v][w] = true;
55 | if (!directed) // 如果不是无向图
56 | g[w][v] = true;
57 | m++;
58 | }
59 |
60 | @Override
61 | public boolean hasEdge(int v, int w) {
62 | assert v >= 0 && v < n;
63 | assert w >= 0 && w < n;
64 | return g[v][w];
65 | }
66 |
67 | @Override
68 | public void show() {
69 | for (int i = 0; i < n; i++) {
70 | for (int j = 0; j < n; j++)
71 | System.out.print(g[i][j] + "\t");
72 | System.out.println();
73 | }
74 | }
75 |
76 | // 返回图中一个顶点的所有邻边
77 | // 由于java使用引用机制,返回一个Vector不会带来额外开销,
78 | @Override
79 | public Iterable adj(int v) {
80 | assert v >= 0 && v < n;
81 | Vector adjv = new Vector<>();
82 | for (int i = 0; i < n; i++) {
83 | if (g[v][i]) {
84 | adjv.add(i);
85 | }
86 | }
87 | return adjv;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/Edge.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: Edge
4 | * Author: Administrator
5 | * Date: 2017/11/15 0015 19:09
6 | * Description: 对有权图的类的边进行封装
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈对有权图的类的边进行封装〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/15 0015
17 | * @since 1.0.0
18 | */
19 | public class Edge implements Comparable {
20 |
21 | private int a, b;
22 | private Weight weight;
23 |
24 | public Edge(int a, int b, Weight weight) {
25 | this.a = a;
26 | this.b = b;
27 | this.weight = weight;
28 | }
29 |
30 | public Edge(Edge e) {
31 | this.a = e.a;
32 | this.b = e.b;
33 | this.weight = e.weight;
34 | }
35 |
36 | public int V() {
37 | return a;
38 | }
39 |
40 | // 返回第二个元素
41 | public int W() {
42 | return b;
43 | }
44 |
45 | public Weight wt() {
46 | return weight;
47 | }
48 |
49 | public int other(int x) {
50 | assert x == a || x == b;
51 | return x == a ? b : a;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "" + a + "-" + b + ": " + weight;
57 | }
58 |
59 | @Override
60 | public int compareTo( Edge that) {
61 | if (weight.compareTo(that.weight) < 0)
62 | return -1;
63 | else if (weight.compareTo(that.weight) > 0)
64 | return +1;
65 | else
66 | return 0;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/Graph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: Graph
4 | * Author: Administrator
5 | * Date: 2017/11/13 0013 20:36
6 | * Description: Graph 图的接口
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈Graph 图的接口〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/13 0013
17 | * @since 1.0.0
18 | */
19 | public interface Graph {
20 | int V();
21 | int E();
22 | void addEdge(int v, int w);
23 | boolean hasEdge(int v, int w);
24 | void show();
25 | Iterable adj(int v);
26 | }
27 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/Path.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: Path
4 | * Author: Administrator
5 | * Date: 2017/11/15 0015 10:01
6 | * Description: Path 图的路径的类
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 |
12 | import java.util.Stack;
13 | import java.util.Vector;
14 |
15 | /**
16 | * 〈一句话功能简述〉
17 | * 〈Path 图的路径的类〉
18 | *
19 | * @author Administrator
20 | * @create 2017/11/15 0015
21 | * @since 1.0.0
22 | */
23 | public class Path {
24 | private Graph G; // 图的引用
25 | private int s; // 起始点
26 | private boolean[] visited; // 记录dfs的过程中节点是否被访问
27 | private int[] from; // 记录路径, from[i]表示查找的路径上i的上一个节点
28 |
29 | private void dfs(int v) {
30 | visited[v] = true;
31 | for (int i : G.adj(v)) {
32 | if (!visited[i]) {
33 | from[i] = v; // 没被访问的节点的值记为v, 存贮在from【i】的数组之中。
34 | dfs(i);
35 | }
36 | }
37 | }
38 |
39 | public Path(Graph graph, int s) {
40 | G = graph;
41 | assert s >= 0 && s < G.V();
42 | visited = new boolean[G.V()];
43 | from = new int[G.V()];
44 | for (int i = 0; i < G.V(); i++) {
45 | visited[i] = false;
46 | from[i] = -1;
47 | }
48 | this.s = s;
49 | // 寻路算法
50 | dfs(s);
51 | }
52 |
53 | // 查询从s点到w点是否有路径
54 | boolean hasPath(int w) {
55 | assert w >= 0 && w < G.V();
56 | return visited[w];
57 | }
58 |
59 | Vector path(int w) {
60 | assert hasPath(w);
61 | Stack s = new Stack<>();
62 | // 通过from数组逆向查找到从s到w的路径, 存放到栈中
63 | int p = w;
64 | while (p != -1) {
65 | s.push(p);
66 | p = from[p];
67 | }
68 |
69 | // 从栈中依次取出元素, 获得顺序的从s到w的路径
70 | Vector res = new Vector();
71 | while (!s.isEmpty())
72 | res.add(s.pop());
73 | return res;
74 | }
75 |
76 | void showPath(int w) {
77 | assert hasPath(w);
78 | Vector vec = path(w);
79 | for (int i = 0; i < vec.size(); i++) {
80 | System.out.print(vec.elementAt(i));
81 | if (i == vec.size() - 1)
82 | System.out.println();
83 | else
84 | System.out.print(" -> ");
85 | }
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/ReadGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: ReadGraph
4 | * Author: Administrator
5 | * Date: 2017/11/13 0013 20:53
6 | * Description: ReadGraph
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | import java.io.*;
12 | import java.util.InputMismatchException;
13 | import java.util.Locale;
14 | import java.util.NoSuchElementException;
15 | import java.util.Scanner;
16 |
17 | /**
18 | * 〈一句话功能简述〉
19 | * 〈ReadGraph 〉
20 | *
21 | * @author Administrator
22 | * @create 2017/11/13 0013
23 | * @since 1.0.0
24 | */
25 | public class ReadGraph {
26 |
27 | private Scanner scanner;
28 |
29 | public ReadGraph(Graph graph, String filename){
30 |
31 | readFile(filename);
32 |
33 | try {
34 | int V = scanner.nextInt();
35 | if (V < 0)
36 | throw new IllegalArgumentException("number of vertices in a Graph must be nonnegative");
37 | assert V == graph.V();
38 |
39 | int E = scanner.nextInt();
40 | if (E < 0)
41 | throw new IllegalArgumentException("number of edges in a Graph must be nonnegative");
42 |
43 | for (int i = 0; i < E; i++) {
44 | int v = scanner.nextInt();
45 | int w = scanner.nextInt();
46 | assert v >= 0 && v < V;
47 | assert w >= 0 && w < V;
48 | graph.addEdge(v, w);
49 | }
50 | }
51 | catch (InputMismatchException e) {
52 | String token = scanner.next();
53 | throw new InputMismatchException("attempts to read an 'int' value from input stream, but the next token is \"" + token + "\"");
54 | }
55 | catch (NoSuchElementException e) {
56 | throw new NoSuchElementException("attempts to read an 'int' value from input stream, but there are no more tokens available");
57 | }
58 | }
59 |
60 | private void readFile(String filename){
61 | assert filename != null;
62 | try {
63 | File file = new File(filename);
64 | if (file.exists()) {
65 | FileInputStream fis = new FileInputStream(file);
66 | scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
67 | scanner.useLocale(Locale.ENGLISH);
68 | }
69 | else
70 | throw new IllegalArgumentException(filename + "doesn't exist.");
71 | }
72 | catch (IOException ioe) {
73 | throw new IllegalArgumentException("Could not open " + filename, ioe);
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/SparseGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: SparseGraph
4 | * Author: Administrator
5 | * Date: 2017/11/12 0012 21:59
6 | * Description: SparseGraph 稀疏图利用邻接表来实现
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | import java.util.Vector;
12 |
13 | /**
14 | * 〈一句话功能简述〉
15 | * 〈SparseGraph 稀疏图利用邻接表来实现〉
16 | *
17 | * @author Administrator
18 | * @create 2017/11/12 0012
19 | * @since 1.0.0
20 | */
21 | public class SparseGraph implements Graph{
22 | private int n; // 节点数
23 | private int m; // 边数
24 | private boolean directed; // 是否为有向图
25 | private Vector[] g; // 图的具体数据
26 |
27 | public SparseGraph(int n, boolean directed) {
28 | assert n > 0;
29 | this.n = n;
30 | this.m = 0; // 初始化没有任何边
31 | this.directed = directed;
32 | // g初始化为n个空的vector, 表示每一个g[i]都为空, 即没有任和边
33 | this.g = new Vector[n];
34 | for (int i = 0; i < n; i++) {
35 | g[i] = new Vector<>();
36 | }
37 | }
38 |
39 | // 返回节点的个数
40 | public int V() {
41 | return n;
42 | }
43 |
44 | // 返回图中边的个数
45 | public int E() {
46 | return m;
47 | }
48 |
49 | public void addEdge(int v, int w) {
50 | assert v >= 0 && v < n;
51 | assert w >= 0 && w < n;
52 | g[v].add(w);
53 | if (!directed) // 如果为无向图,反向也要添加
54 | g[w].add(v);
55 | m++;
56 | }
57 |
58 | public boolean hasEdge(int v, int w) {
59 | assert v >= 0 && v < n;
60 | assert w >= 0 && w < n;
61 | for (int i = 0; i < g[v].size(); i++) {
62 | if (g[v].elementAt(i) == w)
63 | return true;
64 | }
65 | return false;
66 | }
67 |
68 | @Override
69 | public void show() {
70 | for (int i = 0; i < n; i++) {
71 | System.out.print("vertex" + i +"\t");
72 | for (int j = 0; j < g[i].size(); j++) {
73 | System.out.print(g[i].elementAt(j) + "\t");
74 | }
75 | System.out.println();
76 | }
77 | }
78 |
79 | // 返回图中一个顶点的所有邻边
80 | public Iterable adj(int v) {
81 | assert v >= 0 && v < n;
82 | return g[v];
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/SparseWeightedGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: SparseWeightedGraph
4 | * Author: Administrator
5 | * Date: 2017/11/15 0015 19:48
6 | * Description: SparseWeightedGraph
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | import java.util.Vector;
12 |
13 | /**
14 | * 〈一句话功能简述〉
15 | * 〈SparseWeightedGraph〉
16 | *
17 | * @author Administrator
18 | * @create 2017/11/15 0015
19 | * @since 1.0.0
20 | */
21 | public class SparseWeightedGraph implements WeightedGraph {
22 |
23 | private int n; // 节点数
24 | private int m; // 边数
25 | private boolean directed; // 是否为有向图
26 | private Vector>[] g; // 图的具体数据
27 |
28 | // 初始化的过程
29 | public SparseWeightedGraph(int n, boolean directed) {
30 | this.n = n;
31 | this.m = 0;
32 | this.directed = directed;
33 | g = (Vector>[]) new Vector[n];
34 | for (int i = 0; i < n; i++) {
35 | g[i] = new Vector<>();
36 | }
37 | }
38 |
39 | @Override
40 | public int V() {
41 | return n;
42 | }
43 |
44 | @Override
45 | public int E() {
46 | return m;
47 | }
48 |
49 | @Override
50 | // 向图中添加一个边, 权值为weight
51 | public void addEdge(Edge e){
52 |
53 | assert e.V() >= 0 && e.V() < n ;
54 | assert e.W() >= 0 && e.W() < n ;
55 |
56 | // 注意, 由于在邻接表的情况, 查找是否有重边需要遍历整个链表
57 | // 我们的程序允许重边的出现
58 |
59 | g[e.V()].add(new Edge(e));
60 | if( e.V() != e.W() && !directed )
61 | g[e.W()].add(new Edge(e.W(), e.V(), e.wt()));
62 | m ++;
63 | }
64 |
65 | @Override
66 | public boolean hasEdge(int v, int w) {
67 | assert v >= 0 && v < n ;
68 | assert w >= 0 && w < n ;
69 |
70 | for( int i = 0 ; i < g[v].size() ; i ++ )
71 | if( g[v].elementAt(i).other(v) == w )
72 | return true;
73 | return false;
74 | }
75 |
76 | @Override
77 | public void show() {
78 | for( int i = 0 ; i < n ; i ++ ){
79 | System.out.print("vertex " + i + ":\t");
80 | for( int j = 0 ; j < g[i].size() ; j ++ ){
81 | Edge e = g[i].elementAt(j);
82 | System.out.print( "( to:" + e.other(i) + ",wt:" + e.wt() + ")\t");
83 | }
84 | System.out.println();
85 | }
86 | }
87 |
88 | @Override
89 | public Iterable> adj(int v) {
90 | assert v >= 0 && v < n;
91 | return g[v];
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/TestGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: TestGraph
4 | * Author: Administrator
5 | * Date: 2017/11/13 0013 20:59
6 | * Description: nonnegative
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈测试图例子〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/13 0013
17 | * @since 1.0.0
18 | */
19 | public class TestGraph {
20 | public static void main(String[] args) {
21 | String fileName = "E:\\project\\java-Project\\AlgorithmsSolutions\\src\\me\\zonglun\\Graph\\testG1.txt";
22 | SparseGraph g1 = new SparseGraph(13, false);
23 | ReadGraph readGraph1 = new ReadGraph(g1, fileName);
24 | Components components1 = new Components(g1);
25 | System.out.println("test G1 in SparseGraph: " + components1.getCount());
26 | // g1.show();
27 | System.out.println();
28 |
29 | DenseGraph g2 = new DenseGraph(13, false);
30 | ReadGraph readGraph2 = new ReadGraph(g2, fileName);
31 | Components components2 = new Components(g2);
32 | System.out.println("test G1 in Dense Graph:" + components2.getCount());
33 | // g2.show();
34 | System.out.println();
35 |
36 | // 使用两种图的存储方式读取testG2.txt文件
37 | String filename2 = "E:\\project\\java-Project\\AlgorithmsSolutions\\src\\me\\zonglun\\Graph\\testG2.txt";
38 | SparseGraph g3 = new SparseGraph(6, false);
39 | ReadGraph readGraph3 = new ReadGraph(g3, filename2);
40 | Components components3 = new Components(g3);
41 | System.out.println("test G2 in Sparse Graph:" + components3.getCount());
42 | // g3.show();
43 |
44 | System.out.println();
45 |
46 | DenseGraph g4 = new DenseGraph(6, false);
47 | ReadGraph readGraph4 = new ReadGraph(g4, filename2);
48 | System.out.println("test G2 in Dense Graph:");
49 | g4.show();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/TestPath.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: TestPath
4 | * Author: Administrator
5 | * Date: 2017/11/15 0015 10:35
6 | * Description:
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/15 0015
17 | * @since 1.0.0
18 | */
19 | public class TestPath {
20 | public static void main(String[] args) {
21 | String filename = "E:\\project\\java-Project\\AlgorithmsSolutions\\src\\me\\zonglun\\Graph\\testG.txt";
22 | SparseGraph g = new SparseGraph(7, false);
23 | ReadGraph readGraph = new ReadGraph(g, filename);
24 | g.show();
25 | System.out.println();
26 |
27 | Path path = new Path(g, 0);
28 | System.out.println("DFS for path from 0 to 6: ");
29 | path.showPath(6);
30 | ShortestPath spath = new ShortestPath(g,0);
31 | System.out.println("BFS for path from 0 to 6: ");
32 | spath.showPath(6);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/TestWeightedGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: TestWeightedGraph
4 | * Author: Administrator
5 | * Date: 2017/11/15 0015 20:32
6 | * Description: TestWeightedGraph
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈TestWeightedGraph〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/15 0015
17 | * @since 1.0.0
18 | */
19 | public class TestWeightedGraph {
20 | public static void main(String[] args) {
21 |
22 | // 使用两种图的存储方式读取testG1.txt文件
23 | String filename = "E:\\project\\java-Project\\AlgorithmsSolutions\\src\\me\\zonglun\\Graph\\testG1.txt";
24 | SparseWeightedGraph g1 = new SparseWeightedGraph<>(8, false);
25 | ReadWeightedGraph readGraph1 = new ReadWeightedGraph(g1, filename);
26 | System.out.println("test G1 in Sparse Weighted Graph:");
27 | g1.show();
28 |
29 | System.out.println();
30 |
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/WeightedGraph.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C), 2015-2017
3 | * FileName: WeightedGraph
4 | * Author: Administrator
5 | * Date: 2017/11/15 0015 19:46
6 | * Description: WeightedGraph抽象接口
7 | */
8 |
9 | package me.zonglun.Graph;
10 |
11 | /**
12 | * 〈一句话功能简述〉
13 | * 〈WeightedGraph抽象接口〉
14 | *
15 | * @author Administrator
16 | * @create 2017/11/15 0015
17 | * @since 1.0.0
18 | */
19 | public interface WeightedGraph {
20 | int V();
21 |
22 | int E();
23 |
24 | void addEdge(Edge e);
25 |
26 | boolean hasEdge(int v, int w);
27 |
28 | void show();
29 |
30 | Iterable> adj(int v);
31 | }
32 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/testG.txt:
--------------------------------------------------------------------------------
1 | 7 8
2 | 0 1
3 | 0 2
4 | 0 5
5 | 0 6
6 | 3 4
7 | 3 5
8 | 4 5
9 | 4 6
10 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/testG1.txt:
--------------------------------------------------------------------------------
1 | 13 13
2 | 0 5
3 | 4 3
4 | 0 1
5 | 9 12
6 | 6 4
7 | 5 4
8 | 0 2
9 | 11 12
10 | 9 10
11 | 0 6
12 | 7 8
13 | 9 11
14 | 5 3
15 |
--------------------------------------------------------------------------------
/src/me/zonglun/Graph/testG2.txt:
--------------------------------------------------------------------------------
1 | 6 8
2 | 0 1
3 | 0 2
4 | 0 5
5 | 1 2
6 | 1 3
7 | 1 4
8 | 3 4
9 | 3 5
10 |
--------------------------------------------------------------------------------
/src/me/zonglun/Sort/BubbleSort.java:
--------------------------------------------------------------------------------
1 | package me.zonglun.Sort;
2 |
3 | import java.util.Arrays;
4 |
5 | /**
6 | * 〈一句话功能简述〉
7 | * 1. 冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。
8 | * 走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
9 | * 这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
10 | * <1>.比较相邻的元素。如果第一个比第二个大,就交换它们两个;
11 | * <2>.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数;
12 | * <3>.针对所有的元素重复以上的步骤,除了最后一个;
13 | * <4>.重复步骤1~3,直到排序完成。
14 | * you can see more detail in https://blog.zonglun.me/?p=140
15 | * 平均时间复杂度为o(N*2)
16 | *
17 | * @author Administrator
18 | * @create 2017/11/3 0003
19 | * @since 1.0.0
20 | */
21 | public class BubbleSort {
22 | private BubbleSort() {}
23 |
24 | public static void sort(Comparable