Skip to content

Commit 4594e48

Browse files
author
jossc
committed
1.添加基础算法
1 parent 8c44bf9 commit 4594e48

6 files changed

Lines changed: 288 additions & 6 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.algorithm;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName Bubble
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-08-17 21:34
10+
**/
11+
public class Bubble {
12+
private static void sort(int array[]) {
13+
int tmp = 0;
14+
for (int i = 0; i < array.length; i++) {
15+
for (int j = 0; j < array.length - i - 1; j++) {
16+
if (array[j] > array[j + 1]) {
17+
tmp = array[j];
18+
array[j] = array[j + 1];
19+
array[j + 1] = tmp;
20+
}
21+
}
22+
}
23+
}
24+
25+
public static void main(String[] args) {
26+
int[] array = new int[]{5, 8, 6, 3, 9, 2, 1, 7};
27+
sort(array);
28+
System.out.println(Arrays.toString(array));
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.algorithm;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName BubbleSort
7+
* @Author chenzhuo
8+
* @Version 1.0
9+
* @Date 2019-08-17 21:47
10+
**/
11+
public class BubbleSort {
12+
13+
public static void main(String[] args) {
14+
int[] array = new int[]{5, 8, 6, 3, 9, 2, 1, 7};
15+
sort(array);
16+
System.out.println(Arrays.toString(array));
17+
}
18+
19+
public static void sort(int[] array) {
20+
int tmp = 0;
21+
for (int i = 0; i < array.length; i++) {
22+
for (int j = 0; j < array.length - i - 1; j++) {
23+
if (array[j] > array[j + 1]) {
24+
tmp = array[j];
25+
array[j] = array[j + 1];
26+
array[j + 1] = tmp;
27+
}
28+
}
29+
}
30+
}
31+
32+
public static void sort2(int[] array) {
33+
int tmp = 0;
34+
for (int i = 0; i < array.length; i++) {
35+
boolean isSorted = true;
36+
for (int j = 0; j < array.length - i - 1; j++) {
37+
38+
}
39+
}
40+
}
41+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.algorithm;
2+
3+
/**
4+
* @ClassName Merging
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-08-17 14:39
8+
**/
9+
public class Merging {
10+
11+
/**
12+
* 排序
13+
*
14+
* @param array 数字
15+
* @param low 低
16+
* @param high 高
17+
* @return
18+
*/
19+
public static int[] sort(int[] array, int low, int high) {
20+
int mind = (low + high) / 2;
21+
if (low < high) {
22+
sort(array, low, mind);
23+
sort(array, mind + 1, high);
24+
25+
}
26+
return array;
27+
}
28+
29+
/**
30+
* 合并
31+
*
32+
* @param array 数组
33+
* @param low 最低
34+
* @param high 最高
35+
*/
36+
public static void merge(int[] array, int low, int mid, int high) {
37+
int[] temp = new int[high - low + 1];
38+
int i = low;
39+
int j = mid + 1;
40+
int k = 0;
41+
42+
while (i < mid && j <= high) {
43+
if (array[i] <= array[j]) {
44+
temp[k++] = array[i++];
45+
} else {
46+
temp[k++] = array[j++];
47+
}
48+
}
49+
// 把左边剩余的数移入数组
50+
while (i <= mid) {
51+
temp[k++] = array[i++];
52+
}
53+
// 把右边边剩余的数移入数组
54+
while (j <= high) {
55+
temp[k++] = array[j++];
56+
}
57+
// 把新数组中的数覆盖nums数组
58+
for (int x = 0; x < temp.length; x++) {
59+
array[x + low] = temp[x];
60+
}
61+
}
62+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.algorithm;
2+
3+
4+
import java.util.Arrays;
5+
6+
/**
7+
* @CLassName MergingTwo
8+
* @Author chenzhuo
9+
* @Version 1.0
10+
* @Date 2019-08-17 22:37
11+
**/
12+
public class MergingTwo {
13+
14+
15+
/**
16+
* 分割数组
17+
* 按照
18+
* 0+数组长度来分割
19+
*
20+
* @param agrrays
21+
* @param L
22+
* @param r
23+
*/
24+
public static void mergeSort(int[] agrrays, int L, int r) {
25+
if (L == r) {
26+
return;
27+
} else {
28+
int M = (L + r) / 2;
29+
mergeSort(agrrays, L, M);
30+
mergeSort(agrrays, M + 1, r);
31+
merge(agrrays, L, M + 1, r);
32+
}
33+
}
34+
35+
/**
36+
* 合并结果
37+
*
38+
* @param arrays
39+
* @param L
40+
* @param m
41+
* @param r
42+
*/
43+
public static void merge(int[] arrays, int L, int m, int r) {
44+
//这里不能用m-l+1 因为下边的变量j 在右边的数组中可能会越界
45+
int[] left = new int[m - L];
46+
47+
int[] righ = new int[r - m + 1];
48+
49+
for (int i = L; i < m; i++) {
50+
left[i - L] = arrays[i];
51+
}
52+
for (int i = m; i <= r; i++) {
53+
righ[i - m] = arrays[i];
54+
}
55+
int i = 0, j = 0;
56+
int k = L;
57+
//比较这两个数组的值,哪个小,就往数组上放
58+
while (i < left.length && j < righ.length) {
59+
//谁比较小,谁将元素放入大数组中,移动指针,继续比较下一个
60+
if (left[i] < righ[j]) {
61+
arrays[k] = left[i];
62+
i++;
63+
k++;
64+
} else {
65+
arrays[k] = righ[j];
66+
j++;
67+
k++;
68+
}
69+
}
70+
//如果左边的数组还没比较完,右边的数都已经完了,那么将左边的数抄到大数组中(剩下的都是大数字)
71+
while (i < left.length) {
72+
arrays[k] = left[i];
73+
i++;
74+
k++;
75+
}
76+
//如果右边的数组还没比较完,左边的数都已经完了,那么将右边的数抄到大数组中(剩下的都是大数字)
77+
while (j < righ.length) {
78+
arrays[k] = righ[j];
79+
k++;
80+
j++;
81+
}
82+
83+
}
84+
85+
public static void main(String[] args) {
86+
int[] arrays = {-340, 2, 5, 1, 3, 30, 60, 5, 2, 1, 8};
87+
mergeSort(arrays, 0, arrays.length - 1);
88+
System.err.println(Arrays.toString(arrays));
89+
}
90+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.algorithm;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
/**
7+
* @ClassName QuickSort
8+
* @Author chenzhuo
9+
* @Version 1.0
10+
* @Date 2019-08-18 20:06
11+
**/
12+
public class QuickSort {
13+
14+
public static void main(String[] args) {
15+
int[] a = new int[10];
16+
for (int i = 0; i < a.length; i++) {
17+
Random random = new Random();
18+
a[i] = random.nextInt(100);
19+
}
20+
quickSort(a, 0, a.length - 1);
21+
System.err.println(Arrays.toString(a));
22+
}
23+
24+
25+
public static void quickSort(int[] a, int low, int high) {
26+
int pivot;
27+
if (low < high) {
28+
pivot = partition(a, low, high);
29+
quickSort(a, low, pivot - 1);
30+
quickSort(a, pivot + 1, high);
31+
32+
}
33+
}
34+
35+
public static int partition(int[] a, int low, int high) {
36+
int pivokey = a[low];
37+
while (low < high) {
38+
while (high > low && a[high] >= pivokey) {
39+
high--;
40+
}
41+
a[low] = a[high];
42+
while (high > low && a[low] <= pivokey) {
43+
low++;
44+
}
45+
a[high] = a[low];
46+
}
47+
a[low] = pivokey;
48+
return low;
49+
}
50+
51+
}

src/main/java/com/concurrent/thread/threaddemo/ChangeObjectThread.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,35 @@
66
* @Version 1.0
77
* @Date 2019-07-20 23:58
88
**/
9-
public class ChangeObjectThread extends Thread{
9+
public class ChangeObjectThread extends Thread {
1010
public User user;
1111

12-
public ChangeObjectThread(User user){
12+
public ChangeObjectThread(User user) {
1313
this.user = user;
1414
}
1515

1616
@Override
1717
public void run() {
18-
while (true){
19-
synchronized (user){
20-
int v = (int) (System.currentTimeMillis()/1000);
18+
while (true) {
19+
synchronized (user) {
20+
int v = (int) (System.currentTimeMillis() / 1000);
2121
user.setId(v);
2222
try {
2323
Thread.sleep(100);
24-
}catch (InterruptedException e){
24+
} catch (InterruptedException e) {
2525
e.printStackTrace();
2626
}
2727
user.setName(String.valueOf(v));
2828
}
2929
Thread.yield();
3030
}
3131
}
32+
33+
public static void main(String[] args) {
34+
ChangeObjectThread changeObjectThread = new ChangeObjectThread(new User());
35+
Thread thread = new Thread(changeObjectThread);
36+
thread.start();
37+
boolean haveLock = thread.holdsLock(changeObjectThread.user);
38+
System.err.println(haveLock);
39+
}
3240
}

0 commit comments

Comments
 (0)