Skip to content

Commit eb4cccc

Browse files
author
陈伟杰
committed
工厂方法模式 抽象工厂模式 代理模式 适配器模式 策略模式 模板模式 冒泡排序 快速排序 二分查找
1 parent eb64ef6 commit eb4cccc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+824
-76
lines changed

src/main/java/com/chen/algorithm/bsearch/Bsearch.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,36 @@ public int bSearch(int[] array, int high, int low, int target) {
3030
return bSearch(array, low, high, target);
3131
}
3232

33-
return target;
33+
return mid;
3434
}
3535

36+
@Test
37+
public int bSearch(int low, int high, int[] nums, int target) {
38+
39+
if (low > high) {
40+
return -1;
41+
}
42+
43+
int mid = (low + high) / 2;
44+
45+
if (target > nums[mid]) {
46+
low = mid + 1;
47+
return bSearch(low, high, nums, target);
48+
} else if (target < nums[mid]) {
49+
high = mid + 1;
50+
return bSearch(low, high, nums, target);
51+
} else {
52+
return mid;
53+
}
54+
55+
}
56+
57+
58+
59+
60+
61+
62+
63+
3664

3765
}

src/main/java/com/chen/algorithm/sort/SortTest.java renamed to src/main/java/com/chen/algorithm/sort/BubbleSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* 冒泡排序算法
77
* Created by Chen Weijie on 2017/8/17.
88
*/
9-
public class SortTest {
9+
public class BubbleSort {
1010

1111

1212
@Test
13-
public void bubbleSort() {
13+
public void bubbleSort1() {
1414

1515
int[] numbers = {1, 4, 7, 2, 10};
1616

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.chen.algorithm.sort;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* 快速排序
7+
* create by chewj1103
8+
*/
9+
public class QuickSort {
10+
11+
12+
@Test
13+
public void quickSort() {
14+
15+
int[] nums = {3, 7, 2, 10, -1, 4};
16+
quickSort(nums);
17+
for (int num : nums) {
18+
System.out.println(num);
19+
}
20+
}
21+
22+
/**
23+
* 快速排序的方法入口
24+
*
25+
* @param arr 要排序的数组
26+
*/
27+
public static void quickSort(int[] arr) {
28+
qSort(arr, 0, arr.length - 1);
29+
}
30+
31+
private static void qSort(int[] arr, int low, int high) {
32+
if (low < high) {
33+
int pivot = partition(arr, low, high); //将数组分为两部分
34+
qSort(arr, low, pivot - 1); //递归排序左子数组
35+
qSort(arr, pivot + 1, high); //递归排序右子数组
36+
}
37+
}
38+
39+
private static int partition(int[] arr, int low, int high) {
40+
int pivotValue = arr[low]; //枢轴记录
41+
while (low < high) {
42+
while (low < high && arr[high] >= pivotValue) {
43+
--high;
44+
}
45+
arr[low] = arr[high]; //交换比枢轴小的记录到左端
46+
while (low < high && arr[low] <= pivotValue) {
47+
++low;
48+
}
49+
arr[high] = arr[low]; //交换比枢轴小的记录到右端
50+
}
51+
//扫描完成,枢轴到位
52+
arr[low] = pivotValue;
53+
//返回的是枢轴的下标
54+
return low;
55+
}
56+
57+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.chen.algorithm.sort;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* 快速排序练习
7+
*
8+
* @author Chen Weijie
9+
*/
10+
public class QuickSortTest {
11+
12+
13+
public static int partiton(int[] arr, int low, int high) {
14+
15+
int pivotValue = arr[low]; //获取第一个元素为数轴
16+
17+
while (low < high) {
18+
//从high位置往左移动,当arr[high]的元素小于数轴元素,则将arr[high]赋值给arr[low]
19+
while (low < high && arr[high] >= pivotValue) {
20+
high--;
21+
}
22+
arr[low] = arr[high];
23+
//从low位置往右移动,当arr[low]的元素大于数轴元素,则将arr[low]赋值给arr[high]
24+
while (low < high && arr[low] <= pivotValue) {
25+
low++;
26+
}
27+
arr[high] = arr[low];
28+
}
29+
30+
arr[low] = pivotValue;//将数轴元素赋值给arr[low]
31+
return low;
32+
}
33+
34+
35+
public static void quickSort(int arr[], int low, int high) {
36+
37+
if (low < high) {
38+
int pivot = partiton(arr, low, high);
39+
partiton(arr, pivot + 1, high);
40+
partiton(arr, low, pivot - 1);
41+
}
42+
43+
}
44+
45+
46+
public static void quick() {
47+
int[] arr = {9, 2, 58, -10, 40};
48+
quickSort(arr, 0, arr.length - 1);
49+
for (int n : arr) {
50+
System.out.println(n);
51+
}
52+
53+
}
54+
55+
56+
@Test
57+
public void testMain() {
58+
quick();
59+
}
60+
61+
62+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.chen.designPattern.abstractFactory;
2+
3+
/**
4+
* BMW320 产品
5+
* @Author chenweijie
6+
* @Date 2017/8/27 2:26
7+
*/
8+
public class BMW320 implements FactoryBMW {
9+
10+
@Override
11+
public Container getContainer() {
12+
return new ContainerBMW320();
13+
}
14+
15+
@Override
16+
public Engine getEngine() {
17+
return new BMW320Engine();
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.chen.designPattern.abstractFactory;
2+
3+
/**
4+
* 具体产品类1
5+
* @Author chenweijie
6+
* @Date 2017/8/27 2:16
7+
*/
8+
public class BMW320Engine implements Engine {
9+
10+
11+
public BMW320Engine(){
12+
create();
13+
}
14+
15+
@Override
16+
public void create() {
17+
18+
System.out.println("我是320 引擎");
19+
}
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.chen.designPattern.abstractFactory;
2+
3+
/**
4+
* BMW520 产品
5+
* @Author chenweijie
6+
* @Date 2017/8/27 2:24
7+
*/
8+
public class BMW520 implements FactoryBMW {
9+
10+
@Override
11+
public Container getContainer() {
12+
return new ContainerBMW520();
13+
}
14+
15+
@Override
16+
public Engine getEngine() {
17+
return new BMW520Engine();
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.chen.designPattern.abstractFactory;
2+
3+
/** 具体产品类
4+
* @Author chenweijie
5+
* @Date 2017/8/27 2:18
6+
*/
7+
public class BMW520Engine implements Engine {
8+
9+
public BMW520Engine(){
10+
create();
11+
}
12+
13+
@Override
14+
public void create() {
15+
System.out.println("我是520 引擎");
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.chen.designPattern.abstractFactory;
2+
3+
/** 空调抽象产品类
4+
* @Author chenweijie
5+
* @Date 2017/8/27 2:19
6+
*/
7+
public interface Container {
8+
9+
void build();
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.chen.designPattern.abstractFactory;
2+
3+
/**
4+
* 空调具体类
5+
* @Author chenweijie
6+
* @Date 2017/8/27 2:21
7+
*/
8+
public class ContainerBMW320 implements Container {
9+
10+
public ContainerBMW320() {
11+
build();
12+
}
13+
14+
@Override
15+
public void build() {
16+
System.out.println("我是320 空调");
17+
}
18+
}

0 commit comments

Comments
 (0)