Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cae8d8

Browse files
committed
merge sort
1 parent 69e5941 commit 4cae8d8

File tree

7 files changed

+649
-579
lines changed

7 files changed

+649
-579
lines changed

‎src/main/java/grey/algorithm/Code_0001_Gini.java‎

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,52 @@
99
//发生很多很多轮之后,这100人的社会财富分布很均匀吗?
1010
// 可以用基尼系数这个指标来做
1111
public class Code_0001_Gini {
12-
public static void main(String[] args) {
13-
System.out.println("一个社会的基尼系数是一个在0~1之间的小数");
14-
System.out.println("基尼系数为0代表所有人的财富完全一样");
15-
System.out.println("基尼系数为1代表有1个人掌握了全社会的财富");
16-
System.out.println("基尼系数越小,代表社会财富分布越均衡;越大则代表财富分布越不均衡");
17-
System.out.println("在2022年,世界各国的平均基尼系数为0.44");
18-
System.out.println("目前普遍认为,当基尼系数到达 0.5 时");
19-
System.out.println("就意味着社会贫富差距非常大,分布非常不均匀");
20-
System.out.println("社会可能会因此陷入危机,比如大量的犯罪或者经历社会动荡");
21-
System.out.println("测试开始");
22-
int num = 100;
23-
int round = 700000;
24-
System.out.println("人数 " + num);
25-
System.out.println("轮数 " + round);
26-
experiment(num, round);
27-
System.out.println("测试结束");
28-
}
2912

30-
public static void experiment(intnum, intround) {
31-
double[] people = newdouble[num];
32-
Arrays.fill(people, 100d);
33-
for (intn = 1; n <= round; n++) {
34-
for (inti = 0; i < num; i++) {
35-
intgiveTo = i;
36-
do {
37-
giveTo = (int) (Math.random() * num);
38-
} while (giveTo == i);
39-
if (people[i] > 0) {
40-
people[i]--;
41-
people[giveTo]++;
42-
}
43-
}
44-
}
45-
System.out.println("基尼系数为:" + gini(people));
46-
}
13+
public static void main(String[] args) {
14+
System.out.println("一个社会的基尼系数是一个在0~1之间的小数");
15+
System.out.println("基尼系数为0代表所有人的财富完全一样");
16+
System.out.println("基尼系数为1代表有1个人掌握了全社会的财富");
17+
System.out.println("基尼系数越小,代表社会财富分布越均衡;越大则代表财富分布越不均衡");
18+
System.out.println("在2022年,世界各国的平均基尼系数为0.44");
19+
System.out.println("目前普遍认为,当基尼系数到达 0.5 时");
20+
System.out.println("就意味着社会贫富差距非常大,分布非常不均匀");
21+
System.out.println("社会可能会因此陷入危机,比如大量的犯罪或者经历社会动荡");
22+
System.out.println("测试开始");
23+
intnum = 100;
24+
intround = 700000;
25+
System.out.println("人数 " + num);
26+
System.out.println("轮数 " + round);
27+
experiment(num, round);
28+
System.out.println("测试结束");
29+
}
4730

48-
public static double gini(double[] arr) {
49-
double sum = 0d;
50-
double sumAbs = 0d;
51-
for (int i = 0; i < arr.length; i++) {
52-
sum += arr[i];
53-
for (double v : arr) {
54-
sumAbs += Math.abs(arr[i] - v);
55-
}
56-
}
57-
return sumAbs / (sum * 2 * arr.length);
58-
}
59-
}
31+
public static void experiment(int num, int round) {
32+
double[] people = new double[num];
33+
Arrays.fill(people, 100d);
34+
for (int n = 1; n <= round; n++) {
35+
for (int i = 0; i < num; i++) {
36+
int giveTo = i;
37+
do {
38+
giveTo = (int) (Math.random() * num);
39+
} while (giveTo == i);
40+
if (people[i] > 0) {
41+
people[i]--;
42+
people[giveTo]++;
43+
}
44+
}
45+
}
46+
System.out.println("基尼系数为:" + gini(people));
47+
}
48+
49+
public static double gini(double[] arr) {
50+
double sum = 0d;
51+
double sumAbs = 0d;
52+
for (int i = 0; i < arr.length; i++) {
53+
sum += arr[i];
54+
for (double v : arr) {
55+
sumAbs += Math.abs(arr[i] - v);
56+
}
57+
}
58+
return sumAbs / (sum * 2 * arr.length);
59+
}
60+
}

‎src/main/java/grey/algorithm/Code_0003_BubbleSort.java‎

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,83 @@
55
//冒泡排序:0 ~ i范围上,相邻位置较大的数滚下去,最大值最终来到i位置,然后0~i-1范围上继续
66
//笔记:https://www.cnblogs.com/greyzeng/p/15186769.html
77
public class Code_0003_BubbleSort {
8-
public static void bubbleSort(int[] arr) {
9-
if (null == arr || arr.length <= 1) {
10-
return;
11-
}
12-
for (int i = arr.length - 1; i >= 0; i--) {
13-
// 每次搞定i位置的数
14-
for (int j = 0; j < i; j++) {
15-
if (arr[j] > arr[j + 1]) {
16-
swap(arr, j, j + 1);
17-
}
18-
}
19-
}
20-
}
218

22-
public static void swap(int[] arr, int i, int j) {
23-
if (null == arr || arr.length <= 1) {
24-
return;
25-
}
26-
int tmp = arr[i];
27-
arr[i] = arr[j];
28-
arr[j] = tmp;
29-
}
9+
public static void bubbleSort(int[] arr) {
10+
if (null == arr || arr.length <= 1) {
11+
return;
12+
}
13+
for (int i = arr.length - 1; i >= 0; i--) {
14+
// 每次搞定i位置的数
15+
for (int j = 0; j < i; j++) {
16+
if (arr[j] > arr[j + 1]) {
17+
swap(arr, j, j + 1);
18+
}
19+
}
20+
}
21+
}
3022

31-
public static void main(String[] args) {
23+
public static void swap(int[] arr, int i, int j) {
24+
if (null == arr || arr.length <= 1) {
25+
return;
26+
}
27+
int tmp = arr[i];
28+
arr[i] = arr[j];
29+
arr[j] = tmp;
30+
}
3231

33-
// 数组长度1~500,等概率随机
34-
int num = 500;
35-
// 每个值的大小在1~1024,等概率随机
36-
int value = 1024;
37-
// 测试次数
38-
int testTimes = 50000;
39-
System.out.println("测试开始");
40-
for (int i = 0; i < testTimes; i++) {
41-
int[] arr = generateArray(num, value);
42-
int[] copyArray = copyArray(arr);
43-
Arrays.sort(arr);
44-
bubbleSort(copyArray);
45-
if (!sameValue(arr, copyArray)) {
46-
System.out.println("出错了!");
47-
break;
48-
}
49-
}
50-
System.out.println("测试结束");
51-
}
32+
public static void main(String[] args) {
5233

53-
private static boolean sameValue(int[] arr1, int[] arr2) {
54-
if (null == arr1) {
55-
return null != arr2;
56-
}
57-
if (null == arr2) {
58-
return null != arr1;
59-
}
60-
if (arr1.length != arr2.length) {
61-
return false;
62-
}
63-
for (int i = 0; i < arr1.length; i++) {
64-
if (arr1[i] != arr2[i]) {
65-
return false;
66-
}
67-
}
68-
return true;
69-
}
34+
// 数组长度1~500,等概率随机
35+
int num = 500;
36+
// 每个值的大小在1~1024,等概率随机
37+
int value = 1024;
38+
// 测试次数
39+
int testTimes = 50000;
40+
System.out.println("测试开始");
41+
for (int i = 0; i < testTimes; i++) {
42+
int[] arr = generateArray(num, value);
43+
int[] copyArray = copyArray(arr);
44+
Arrays.sort(arr);
45+
bubbleSort(copyArray);
46+
if (!sameValue(arr, copyArray)) {
47+
System.out.println("出错了!");
48+
break;
49+
}
50+
}
51+
System.out.println("测试结束");
52+
}
7053

71-
private static int[] generateArray(int num, int value) {
72-
int[] arr = new int[(int) (Math.random() * num) + 1];
73-
for (int i = 0; i < arr.length; i++) {
74-
arr[i] = (int) (Math.random() * value) + 1;
75-
}
76-
return arr;
77-
}
54+
private static boolean sameValue(int[] arr1, int[] arr2) {
55+
if (null == arr1) {
56+
return null != arr2;
57+
}
58+
if (null == arr2) {
59+
return null != arr1;
60+
}
61+
if (arr1.length != arr2.length) {
62+
return false;
63+
}
64+
for (int i = 0; i < arr1.length; i++) {
65+
if (arr1[i] != arr2[i]) {
66+
return false;
67+
}
68+
}
69+
return true;
70+
}
7871

79-
private static int[] copyArray(int[] arr) {
80-
int[] copyArray = new int[arr.length];
81-
for (int i = 0; i < copyArray.length; i++) {
82-
copyArray[i] = arr[i];
83-
}
84-
return copyArray;
85-
}
72+
private static int[] generateArray(int num, int value) {
73+
int[] arr = new int[(int) (Math.random() * num) + 1];
74+
for (int i = 0; i < arr.length; i++) {
75+
arr[i] = (int) (Math.random() * value) + 1;
76+
}
77+
return arr;
78+
}
79+
80+
private static int[] copyArray(int[] arr) {
81+
int[] copyArray = new int[arr.length];
82+
for (int i = 0; i < copyArray.length; i++) {
83+
copyArray[i] = arr[i];
84+
}
85+
return copyArray;
86+
}
8687
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /